﻿using ICSharpCode.SharpZipLib.Zip;
using IOA.MES.Business;
using IOA.MES.Common;
using IOA.MES.DataAccess.Enum;
using IOA.MES.DataAccess.VModel;
using IOA.MES.Log.Business;
using IOA.MES.Web.App_Start;
using IOA.Utils;
using Newtonsoft.Json;
using NLog;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace IOA.MES.Web.Controllers
{
    public class ReportController : Controller
    {
        private Logger logger = LogManager.GetCurrentClassLogger();

        #region 组件查询
        /// <summary>
        /// 组件检索
        /// </summary>
        public ActionResult ComponentSearch()
        {
            return View();
        }

        /// <summary>
        /// 查询组件列表
        /// </summary>
        [HttpPost]
        public JsonResult QueryComponents(int page, int limit, string orderNo, string workOrderNo, string cabinetNo, string packNo, string barCode)
        {
            var total = 0;
            var components = new ComponentMgr().QueryComponents(page, limit, out total, orderNo, workOrderNo, cabinetNo, packNo, barCode);
            return Json(new ResultJson<object>(components, total));
        }

        /// <summary>
        /// 导出组件列表
        /// </summary>
        [HttpPost]
        public void ExportComponents(string orderNo, string workOrderNo, string cabinetNo, string packNo, string barCode)
        {
            var book = new XSSFWorkbook();
            var sheet = book.CreateSheet("组件列表");
            for (int i = 0; i <= 105; i++)
            {
                sheet.SetColumnWidth(i, 20 * 256 + 200);
            }
            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            #region Header
            var rowHeader = sheet.CreateRow(0);
            rowHeader.Height = 30 * 20;
            rowHeader.CreateCell(0).SetCellValue("序号");
            rowHeader.CreateCell(1).SetCellValue("组件条码");
            rowHeader.CreateCell(2).SetCellValue("订单号");
            rowHeader.CreateCell(3).SetCellValue("工单号");
            rowHeader.CreateCell(4).SetCellValue("客户代码");
            rowHeader.CreateCell(5).SetCellValue("客户名称");
            rowHeader.CreateCell(6).SetCellValue("成品料号");
            rowHeader.CreateCell(7).SetCellValue("成品名称");
            rowHeader.CreateCell(8).SetCellValue("生产车间");
            rowHeader.CreateCell(9).SetCellValue("流水线");
            rowHeader.CreateCell(10).SetCellValue("最后站点");
            rowHeader.CreateCell(11).SetCellValue("分片时间");
            rowHeader.CreateCell(12).SetCellValue("焊接时间");
            rowHeader.CreateCell(13).SetCellValue("焊接班次");
            rowHeader.CreateCell(14).SetCellValue("焊接站点");
            rowHeader.CreateCell(15).SetCellValue("焊接账号");
            rowHeader.CreateCell(16).SetCellValue("叠层时间");
            rowHeader.CreateCell(17).SetCellValue("叠层班次");
            rowHeader.CreateCell(18).SetCellValue("叠层站点");
            rowHeader.CreateCell(19).SetCellValue("叠层账号");
            rowHeader.CreateCell(20).SetCellValue("EL1时间");
            rowHeader.CreateCell(21).SetCellValue("EL1班次");
            rowHeader.CreateCell(22).SetCellValue("EL1站点");
            rowHeader.CreateCell(23).SetCellValue("EL1账号");
            rowHeader.CreateCell(24).SetCellValue("层压时间");
            rowHeader.CreateCell(25).SetCellValue("层压班次");
            rowHeader.CreateCell(26).SetCellValue("层压站点");
            rowHeader.CreateCell(27).SetCellValue("层压位置");
            rowHeader.CreateCell(28).SetCellValue("层压账号");
            rowHeader.CreateCell(29).SetCellValue("NQC时间");
            rowHeader.CreateCell(30).SetCellValue("NQC班次");
            rowHeader.CreateCell(31).SetCellValue("NQC站点");
            rowHeader.CreateCell(32).SetCellValue("NQC账号");
            rowHeader.CreateCell(33).SetCellValue("装框时间");
            rowHeader.CreateCell(34).SetCellValue("装框班次");
            rowHeader.CreateCell(35).SetCellValue("装框站点");
            rowHeader.CreateCell(36).SetCellValue("装框账号");
            rowHeader.CreateCell(37).SetCellValue("清洗时间");
            rowHeader.CreateCell(38).SetCellValue("清洗班次");
            rowHeader.CreateCell(39).SetCellValue("清洗站点");
            rowHeader.CreateCell(40).SetCellValue("清洗账号");
            rowHeader.CreateCell(41).SetCellValue("IV时间");
            rowHeader.CreateCell(42).SetCellValue("IV班次");
            rowHeader.CreateCell(43).SetCellValue("IV站点");
            rowHeader.CreateCell(44).SetCellValue("IV测试账号");
            rowHeader.CreateCell(45).SetCellValue("绝缘耐压时间");
            rowHeader.CreateCell(46).SetCellValue("绝缘耐压班次");
            rowHeader.CreateCell(47).SetCellValue("绝缘耐压站点");
            rowHeader.CreateCell(48).SetCellValue("绝缘耐压账号");
            rowHeader.CreateCell(49).SetCellValue("EL2时间");
            rowHeader.CreateCell(50).SetCellValue("EL2班次");
            rowHeader.CreateCell(51).SetCellValue("EL2站点");
            rowHeader.CreateCell(52).SetCellValue("EL2账号");
            rowHeader.CreateCell(53).SetCellValue("铭牌打印时间");
            rowHeader.CreateCell(54).SetCellValue("铭牌打印班次");
            rowHeader.CreateCell(55).SetCellValue("铭牌打印站点");
            rowHeader.CreateCell(56).SetCellValue("铭牌打印账号");
            rowHeader.CreateCell(57).SetCellValue("终检时间");
            rowHeader.CreateCell(58).SetCellValue("终检班次");
            rowHeader.CreateCell(59).SetCellValue("终检站点");
            rowHeader.CreateCell(60).SetCellValue("终检账号");
            rowHeader.CreateCell(61).SetCellValue("打包时间");
            rowHeader.CreateCell(62).SetCellValue("打包班次");
            rowHeader.CreateCell(63).SetCellValue("打包站点");
            rowHeader.CreateCell(64).SetCellValue("打包账号");
            rowHeader.CreateCell(65).SetCellValue("电池片颜色");
            rowHeader.CreateCell(66).SetCellValue("组件颜色");
            rowHeader.CreateCell(67).SetCellValue("背板厂家");
            rowHeader.CreateCell(68).SetCellValue("背板规格");
            rowHeader.CreateCell(69).SetCellValue("接线盒厂家");
            rowHeader.CreateCell(70).SetCellValue("接线盒规格");
            rowHeader.CreateCell(71).SetCellValue("边框厂家");
            rowHeader.CreateCell(72).SetCellValue("边框规格");
            rowHeader.CreateCell(73).SetCellValue("电池片厂家");
            rowHeader.CreateCell(74).SetCellValue("电池片规格");
            rowHeader.CreateCell(75).SetCellValue("电池片箱号");
            rowHeader.CreateCell(76).SetCellValue("面积");
            rowHeader.CreateCell(77).SetCellValue("效率");
            rowHeader.CreateCell(78).SetCellValue("单片功率");
            rowHeader.CreateCell(79).SetCellValue("理论功率");
            rowHeader.CreateCell(80).SetCellValue("转换效率");
            rowHeader.CreateCell(81).SetCellValue("EL1一次合格");
            rowHeader.CreateCell(82).SetCellValue("NQC一次合格");
            rowHeader.CreateCell(83).SetCellValue("EL2一次合格");
            rowHeader.CreateCell(84).SetCellValue("终检一次合格");
            rowHeader.CreateCell(85).SetCellValue("等级");
            rowHeader.CreateCell(86).SetCellValue("铭牌型号");
            rowHeader.CreateCell(87).SetCellValue("额定功率");
            rowHeader.CreateCell(88).SetCellValue("档位");
            rowHeader.CreateCell(89).SetCellValue("Isc");
            rowHeader.CreateCell(90).SetCellValue("Voc");
            rowHeader.CreateCell(91).SetCellValue("Vm");
            rowHeader.CreateCell(92).SetCellValue("Im");
            rowHeader.CreateCell(93).SetCellValue("FF");
            rowHeader.CreateCell(94).SetCellValue("Pmax");
            rowHeader.CreateCell(95).SetCellValue("Temp");
            rowHeader.CreateCell(96).SetCellValue("Ress");
            rowHeader.CreateCell(97).SetCellValue("Ressh");
            rowHeader.CreateCell(98).SetCellValue("ModEff");
            rowHeader.CreateCell(99).SetCellValue("SunRef");
            rowHeader.CreateCell(100).SetCellValue("生产状态");
            rowHeader.CreateCell(101).SetCellValue("打包编号");
            rowHeader.CreateCell(102).SetCellValue("柜号");
            rowHeader.CreateCell(103).SetCellValue("仓库");
            rowHeader.CreateCell(104).SetCellValue("库位");
            rowHeader.CreateCell(105).SetCellValue("入库时间");
            rowHeader.CreateCell(106).SetCellValue("出库时间");
            rowHeader.CreateCell(107).SetCellValue("上车时间");
            #endregion

            if (string.IsNullOrWhiteSpace(workOrderNo) && string.IsNullOrWhiteSpace(cabinetNo) && string.IsNullOrWhiteSpace(packNo) && string.IsNullOrWhiteSpace(barCode))
            {
                var ms1 = new MemoryStream();
                book.Write(ms1);
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"{DateTime.Now.ToString("yyMMddHHmmss")}_组件查询"));
                Response.BinaryWrite(ms1.ToArray());
                book = null;
                ms1.Close();
                ms1.Dispose();
                return;
            }

            var total = 0;
            var components = new ComponentMgr().QueryComponents(1, 0, out total, orderNo, workOrderNo, cabinetNo, packNo, barCode);

            var rowIndex = 1;
            foreach (var item in components)
            {
                var tmpRow = sheet.CreateRow(rowIndex);
                tmpRow.CreateCell(0).SetCellValue(rowIndex);
                tmpRow.CreateCell(1).SetCellValue(item.BarCode);
                tmpRow.CreateCell(2).SetCellValue(item.OrderNo);
                tmpRow.CreateCell(3).SetCellValue(item.WorkOrderNo);
                tmpRow.CreateCell(4).SetCellValue(item.CustomerCode);
                tmpRow.CreateCell(5).SetCellValue(item.CustomerName);
                tmpRow.CreateCell(6).SetCellValue(item.ProductCode);
                tmpRow.CreateCell(7).SetCellValue(item.ProductName);
                tmpRow.CreateCell(8).SetCellValue(item.WorkshopName);
                tmpRow.CreateCell(9).SetCellValue(item.LineName);
                tmpRow.CreateCell(10).SetCellValue(item.CNLastSite);
                tmpRow.CreateCell(11).SetCellValue(item.DistributeTime?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(12).SetCellValue(item.WeldingTime?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(13).SetCellValue(item.WeldingDuty);
                tmpRow.CreateCell(14).SetCellValue(item.WeldingMachine);
                tmpRow.CreateCell(15).SetCellValue(item.WeldingUser);
                tmpRow.CreateCell(16).SetCellValue(item.LaminatedTime?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(17).SetCellValue(item.LaminatedDuty);
                tmpRow.CreateCell(18).SetCellValue(item.LaminatedMachine);
                tmpRow.CreateCell(19).SetCellValue(item.LaminatedUser);
                tmpRow.CreateCell(20).SetCellValue(item.EL1Time?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(21).SetCellValue(item.EL1Duty);
                tmpRow.CreateCell(22).SetCellValue(item.EL1Machine);
                tmpRow.CreateCell(23).SetCellValue(item.EL1User);
                tmpRow.CreateCell(24).SetCellValue(item.TopTime?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(25).SetCellValue(item.TopDuty);
                tmpRow.CreateCell(26).SetCellValue(item.TopMachine);
                tmpRow.CreateCell(27).SetCellValue(item.TopPosition);
                tmpRow.CreateCell(28).SetCellValue(item.TopUser);
                tmpRow.CreateCell(29).SetCellValue(item.NQCTime?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(30).SetCellValue(item.NQCDuty);
                tmpRow.CreateCell(31).SetCellValue(item.NQCMachine);
                tmpRow.CreateCell(32).SetCellValue(item.NQCUser);
                tmpRow.CreateCell(33).SetCellValue(item.FramingTime?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(34).SetCellValue(item.FramingDuty);
                tmpRow.CreateCell(35).SetCellValue(item.FramingMachine);
                tmpRow.CreateCell(36).SetCellValue(item.FramingUser);
                tmpRow.CreateCell(37).SetCellValue(item.CleanTime?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(38).SetCellValue(item.CleanDuty);
                tmpRow.CreateCell(39).SetCellValue(item.CleanMachine);
                tmpRow.CreateCell(40).SetCellValue(item.CleanUser);
                tmpRow.CreateCell(41).SetCellValue(item.IVTestTime?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(42).SetCellValue(item.IVTestDuty);
                tmpRow.CreateCell(43).SetCellValue(item.IVTestMachine);
                tmpRow.CreateCell(44).SetCellValue(item.IVTestUser);
                tmpRow.CreateCell(45).SetCellValue(item.InsulatedResistanceTime?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(46).SetCellValue(item.InsulatedResistanceDuty);
                tmpRow.CreateCell(47).SetCellValue(item.InsulatedResistanceMachine);
                tmpRow.CreateCell(48).SetCellValue(item.InsulatedResistanceUser);
                tmpRow.CreateCell(49).SetCellValue(item.EL2Time?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(50).SetCellValue(item.EL2Duty);
                tmpRow.CreateCell(51).SetCellValue(item.EL2Machine);
                tmpRow.CreateCell(52).SetCellValue(item.EL2User);
                tmpRow.CreateCell(53).SetCellValue(item.NameplatePrintingTime?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(54).SetCellValue(item.NameplatePrintingDuty);
                tmpRow.CreateCell(55).SetCellValue(item.NameplatePrintingMachine);
                tmpRow.CreateCell(56).SetCellValue(item.NameplatePrintingUser);
                tmpRow.CreateCell(57).SetCellValue(item.FinalInspectionTime?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(58).SetCellValue(item.FinalInspectionDuty);
                tmpRow.CreateCell(59).SetCellValue(item.FinalInspectionMachine);
                tmpRow.CreateCell(60).SetCellValue(item.FinalInspectionUser);
                tmpRow.CreateCell(61).SetCellValue(item.PackTime?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(62).SetCellValue(item.PackDuty);
                tmpRow.CreateCell(63).SetCellValue(item.PackMachine);
                tmpRow.CreateCell(64).SetCellValue(item.PackUser);
                tmpRow.CreateCell(65).SetCellValue(item.CellColor);
                tmpRow.CreateCell(66).SetCellValue(item.ComponentColor);
                tmpRow.CreateCell(67).SetCellValue(item.BackPlanFactory);
                tmpRow.CreateCell(68).SetCellValue(item.BackPlanSpecs);
                tmpRow.CreateCell(69).SetCellValue(item.JunBoxFactory);
                tmpRow.CreateCell(70).SetCellValue(item.JunBoxSpecs);
                tmpRow.CreateCell(71).SetCellValue(item.AluFrameFactory);
                tmpRow.CreateCell(72).SetCellValue(item.AluFrameSpecs);
                tmpRow.CreateCell(73).SetCellValue(item.BatteryFactory);
                tmpRow.CreateCell(74).SetCellValue(item.BatterySpecs);
                tmpRow.CreateCell(75).SetCellValue(item.CellBoxNo);
                tmpRow.CreateCell(76).SetCellValue((double)item.CellArea);
                tmpRow.CreateCell(77).SetCellValue((double)item.CellEfficiency);
                tmpRow.CreateCell(78).SetCellValue((double)item.SinglePiecePower);
                tmpRow.CreateCell(79).SetCellValue((double)item.TheoreticalPower);
                tmpRow.CreateCell(80).SetCellValue((double)item.ConversionEfficiency);
                tmpRow.CreateCell(81).SetCellValue(!item.EL1OncePass.HasValue ? "" : (item.EL1OncePass == true ? "是" : "否"));
                tmpRow.CreateCell(82).SetCellValue(!item.NQCOncePass.HasValue ? "" : (item.NQCOncePass == true ? "是" : "否"));
                tmpRow.CreateCell(83).SetCellValue(!item.EL2OncePass.HasValue ? "" : (item.EL2OncePass == true ? "是" : "否"));
                tmpRow.CreateCell(84).SetCellValue(!item.FinalInspectionOncePass.HasValue ? "" : (item.FinalInspectionOncePass == true ? "是" : "否"));
                tmpRow.CreateCell(85).SetCellValue(item.QLevel);
                tmpRow.CreateCell(86).SetCellValue(item.NameplateModel);
                tmpRow.CreateCell(87).SetCellValue((double)(item.IVPowerLevel ?? 0));
                tmpRow.CreateCell(88).SetCellValue(item.IVCurrentLevel);
                tmpRow.CreateCell(89).SetCellValue((double)(item.Isc ?? 0));
                tmpRow.CreateCell(90).SetCellValue((double)(item.Voc ?? 0));
                tmpRow.CreateCell(91).SetCellValue((double)(item.Vm ?? 0));
                tmpRow.CreateCell(92).SetCellValue((double)(item.Im ?? 0));
                tmpRow.CreateCell(93).SetCellValue((double)(item.FF ?? 0));
                tmpRow.CreateCell(94).SetCellValue((double)(item.Pmax ?? 0));
                tmpRow.CreateCell(95).SetCellValue((double)(item.Temp ?? 0));
                tmpRow.CreateCell(96).SetCellValue((double)(item.Ress ?? 0));
                tmpRow.CreateCell(97).SetCellValue((double)(item.Ressh ?? 0));
                tmpRow.CreateCell(98).SetCellValue((double)(item.ModEff ?? 0));
                tmpRow.CreateCell(99).SetCellValue((double)(item.SunRef ?? 0));
                tmpRow.CreateCell(100).SetCellValue(item.CNProduceStatus);
                tmpRow.CreateCell(101).SetCellValue(item.PackNo);
                tmpRow.CreateCell(102).SetCellValue(item.CabinetNo);
                tmpRow.CreateCell(103).SetCellValue(item.WarehouseName);
                tmpRow.CreateCell(104).SetCellValue(item.Location);
                tmpRow.CreateCell(105).SetCellValue(item.InboundTime?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(106).SetCellValue(item.OutboundTime?.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(107).SetCellValue(item.LoadTime?.ToString("yyyy-MM-dd HH:mm:ss"));

                for (int i = 0; i < 106; i++)
                {
                    tmpRow.GetCell(i).CellStyle = styleCenter;
                }
                rowIndex++;
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"{DateTime.Now.ToString("yyMMddHHmmss")}_组件查询"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
        }

        /// <summary>
        /// 组件详情
        /// </summary>
        public ActionResult ComponentDetail(string barCode)
        {
            var component = new ComponentMgr().GetComponent(barCode);
            return View(component);
        }

        /// <summary>
        /// 查询操作日志
        /// </summary>
        [HttpPost]
        public JsonResult QueryOprLogs(string subjectType, string subjectNo)
        {
            var logs = new OprLogMgr().QueryLogs(subjectType, subjectNo);
            return Json(new ResultJson<object>(logs, logs.Count));
        }

        /// <summary>
        /// 查询焊机参数
        /// </summary>
        [HttpPost]
        public JsonResult QueryWeldingDatas(string barCode)
        {
            var datas = new ProductViaSiteMgr().QueryWeldingDatas(barCode);
            return Json(new ResultJson<object>(datas, datas.Count));
        }

        /// <summary>
        /// 查询层压参数
        /// </summary>
        [HttpPost]
        public JsonResult QueryTopDatas(string barCode)
        {
            var datas = new ProductViaSiteMgr().QueryTopDatas(barCode);
            return Json(new ResultJson<object>(datas, datas.Count));
        }

        /// <summary>
        /// 查询Iv参数
        /// </summary>
        [HttpPost]
        public JsonResult QueryIvDatas(string barCode)
        {
            var datas = new ProductViaSiteMgr().QueryIvDatas(barCode);
            return Json(new ResultJson<object>(datas, datas.Count));
        }

        /// <summary>
        /// 查询绝缘耐压参数
        /// </summary>
        [HttpPost]
        public JsonResult QueryInsulateDatas(string barCode)
        {
            var datas = new ProductViaSiteMgr().QueryInsulateDatas(barCode);
            return Json(new ResultJson<object>(datas, datas.Count));
        }

        /// <summary>
        /// 查询Iv参数
        /// </summary>
        [HttpPost]
        public JsonResult QueryViaSiteImages(string barCode)
        {
            var datas = new ProductViaSiteMgr().QueryViaSiteImages(barCode);
            return Json(new ResultJson<object>(datas, datas.Count));
        }
        #endregion

        #region 过站信息查询
        /// <summary>
        /// 过站检索
        /// </summary>
        public ActionResult ViaSiteSearch()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            ViewBag.SiteTypes = new CommonMgr().QueryEnumTypes(typeof(SiteType));
            //ViewBag.OrderNo = new OrderMgr().QueryOrders();
            return View();
        }

        /// <summary>
        /// 查询过站记录
        /// </summary>
        [HttpPost]
        public JsonResult QueryViaSiteRecords(int page, int limit, string workshopCode, int lineId, SiteType? siteType, string orderNo, string workOrderNo, string barCode, DateTime startTime, DateTime endTime)
        {
            int total = 0;
            var userWorkShops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            if (!userWorkShops.Any(_ => StringUtils.Equals(_.WorkshopCode, workshopCode)))
            {
                return Json(new ResultJson<object>(new List<object>(), total));
            }
            var records = new ProductViaSiteMgr().QueryViaSiteRecords(page, limit, out total, new List<string> { workshopCode }, lineId, siteType, orderNo, workOrderNo, barCode, startTime, endTime);
            return Json(new ResultJson<object>(records, total));
        }

        /// <summary>
        /// 导出过站记录
        /// </summary>
        public void ExportViaSiteRecords(string workshopCode, int lineId, SiteType? siteType, string orderNo, string workOrderNo, string barCode, DateTime startTime, DateTime endTime)
        {
            if ((endTime - startTime).TotalDays > 7)
            {
                Response.Write("<script>alert('最长导出时间不得大于7天');</script>");
                return;
            }
            var book = new XSSFWorkbook();
            var sheet = book.CreateSheet("过站记录");
            for (int i = 0; i <= 12; i++)
            {
                sheet.SetColumnWidth(i, 8 * 300 + 200);
            }
            sheet.SetColumnWidth(1, 8 * 800 + 200);
            sheet.SetColumnWidth(3, 8 * 600 + 200);
            sheet.SetColumnWidth(4, 8 * 800 + 200);
            sheet.SetColumnWidth(6, 8 * 800 + 200);
            sheet.SetColumnWidth(7, 8 * 800 + 200);

            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            var row1 = sheet.CreateRow(0);
            row1.Height = 30 * 20;
            row1.CreateCell(0).SetCellValue("序号");
            row1.CreateCell(1).SetCellValue("组件条码");
            row1.CreateCell(2).SetCellValue("过站类型");
            row1.CreateCell(3).SetCellValue("过站设备");
            row1.CreateCell(4).SetCellValue("过站时间");
            row1.CreateCell(5).SetCellValue("生产班次");
            row1.CreateCell(6).SetCellValue("订单号");
            row1.CreateCell(7).SetCellValue("工单号");
            row1.CreateCell(8).SetCellValue("层压位置");
            row1.CreateCell(9).SetCellValue("判定等级");
            row1.CreateCell(10).SetCellValue("异常原因");
            row1.CreateCell(11).SetCellValue("异常数量");
            row1.CreateCell(12).SetCellValue("异常坐标");
            for (int i = 0; i <= 12; i++)
            {
                row1.GetCell(i).CellStyle = styleCenter;
            }

            int total = 0;
            var userWorkShops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            if (!userWorkShops.Any(_ => StringUtils.Equals(_.WorkshopCode, workshopCode)))
            {
                var ms1 = new MemoryStream();
                book.Write(ms1);
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"过站记录（{startTime:yyyyMMddHHmm}-{endTime:yyyyMMddHHmm}）"));
                Response.BinaryWrite(ms1.ToArray());
                book = null;
                ms1.Close();
                ms1.Dispose();
                return;
            }
            var report = new ProductViaSiteMgr().QueryViaSiteRecords(1, 0, out total, new List<string> { workshopCode }, lineId, siteType, orderNo, workOrderNo, barCode, startTime, endTime);

            if (!report.Any())
            {
                var ms1 = new MemoryStream();
                book.Write(ms1);
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"过站记录（{startTime:yyyyMMddHHmm}-{endTime:yyyyMMddHHmm}）"));
                Response.BinaryWrite(ms1.ToArray());
                book = null;
                ms1.Close();
                ms1.Dispose();
                return;
            }

            for (int i = 0; i < report.Count; i++)
            {
                var tmpRow = sheet.CreateRow(i + 1);
                tmpRow.Height = 30 * 20;
                tmpRow.CreateCell(0).SetCellValue(i + 1);
                tmpRow.CreateCell(1).SetCellValue(report[i].Code);
                tmpRow.CreateCell(2).SetCellValue(report[i].CNSiteType);
                tmpRow.CreateCell(3).SetCellValue(report[i].MachineName);
                tmpRow.CreateCell(4).SetCellValue(report[i].CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(5).SetCellValue(report[i].DutyName);
                tmpRow.CreateCell(6).SetCellValue(report[i].OrderNo);
                tmpRow.CreateCell(7).SetCellValue(report[i].WorkOrderNo);
                tmpRow.CreateCell(8).SetCellValue(report[i].TopPosition);
                tmpRow.CreateCell(9).SetCellValue(new[] { SiteType.EL1, SiteType.EL2, SiteType.NQC, SiteType.FinalInspection }.Contains(report[i].SiteType) ? report[i].QLevel : string.Empty);
                tmpRow.CreateCell(10).SetCellValue(report[i].BadReason);
                tmpRow.CreateCell(11).SetCellValue(report[i].BadCount <= 0 ? string.Empty : report[i].BadCount.ToString());
                tmpRow.CreateCell(12).SetCellValue(report[i].BadCoordinates);
                for (int j = 0; j <= 12; j++)
                {
                    tmpRow.GetCell(j).CellStyle = styleCenter;
                }
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"过站记录（{startTime:yyyyMMddHHmm}-{endTime:yyyyMMddHHmm}）"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
        }
        #endregion

        #region 生产报表
        /// <summary>
        /// 生产数据
        /// </summary>
        public ActionResult Production()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 查询流水线列表
        /// </summary>
        [HttpPost]
        public JsonResult QueryLines(string workshopCode)
        {
            var lines = new AssemblyLineMgr().QueryLines(workshopCode);
            return Json(new ResultJson<object>(lines));
        }

        /// <summary>
        /// 统计过站记录
        /// </summary>
        [HttpPost]
        public JsonResult QueryViaSiteStatistics(string workshopCode, int lineId, string orderNo, string workOrderNo, DateTime startTime, DateTime endTime)
        {
            var report = new ReportMgr().QueryViaSiteStatistics(workshopCode, lineId, orderNo, workOrderNo, startTime, endTime);
            return Json(new ResultJson<object>(report, report.Count));
        }

        /// <summary>
        /// 合格率统计
        /// </summary>
        [HttpPost]
        public JsonResult QueryPassRates(string workshopCode, int lineId, string orderNo, string workOrderNo, DateTime startTime, DateTime endTime)
        {
            var report = new ReportMgr().QueryPassRates(workshopCode, lineId, orderNo, workOrderNo, startTime, endTime);
            return Json(new ResultJson<object>(report, report.Count));
        }

        /// <summary>
        /// 碎片率统计
        /// </summary>
        [HttpPost]
        public JsonResult QueryCompenRates(string workshopCode, string orderNo, string workOrderNo, DateTime startTime, DateTime endTime)
        {
            var report = new ReportMgr().QueryCompenRates(workshopCode, orderNo, workOrderNo, startTime, endTime);
            return Json(new ResultJson<object>(report, report.Count));
        }

        /// <summary>
        /// IV分布统计
        /// </summary>
        [HttpPost]
        public JsonResult QueryIvDistributes(string workshopCode, int lineId, string orderNo, string workOrderNo, DateTime startTime, DateTime endTime)
        {
            var report = new ReportMgr().QueryIvDistributes(workshopCode, lineId, orderNo, workOrderNo, startTime, endTime);
            return Json(new ResultJson<object>(report, report.Count));
        }

        /// <summary>
        /// 导出生产数据
        /// </summary>
        public void ExportProductionReports(string workshopCode, int lineId, string orderNo, string workOrderNo, DateTime startTime, DateTime endTime)
        {
            var viaSiteStatistics = new ReportMgr().QueryViaSiteStatistics(workshopCode, lineId, orderNo, workOrderNo, startTime, endTime);
            var passRates = new ReportMgr().QueryPassRates(workshopCode, lineId, orderNo, workOrderNo, startTime, endTime);
            var ivDistributes = new ReportMgr().QueryIvDistributes(workshopCode, lineId, orderNo, workOrderNo, startTime, endTime);
            var workshop = new WorkshopMgr().GetWorkshop(workshopCode);

            var book = new XSSFWorkbook();
            var sheet = book.CreateSheet("生产报表");
            for (int i = 0; i < 11; i++)
            {
                sheet.SetColumnWidth(i, 8 * 256 + 200);
            }
            sheet.SetColumnWidth(1, 8 * 500 + 200);
            sheet.SetColumnWidth(6, 8 * 600 + 200);

            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            var row0 = sheet.CreateRow(0);
            row0.Height = 30 * 20;
            row0.CreateCell(0).SetCellValue("序号");
            row0.CreateCell(1).SetCellValue("工序");
            row0.CreateCell(2).SetCellValue("等级");
            row0.CreateCell(3).SetCellValue("产量");
            for (int i = 0; i < 4; i++)
            {
                row0.GetCell(i).CellStyle = styleCenter;
            }

            for (int i = 0; i < viaSiteStatistics.Count; i++)
            {
                var tmpRow = sheet.CreateRow(i + 1);
                tmpRow.Height = 30 * 20;
                tmpRow.CreateCell(0).SetCellValue(i + 1);
                tmpRow.CreateCell(1).SetCellValue(viaSiteStatistics[i].SiteType);
                tmpRow.CreateCell(2).SetCellValue(viaSiteStatistics[i].QLevel);
                tmpRow.CreateCell(3).SetCellValue(viaSiteStatistics[i].Count);

                for (int j = 0; j < 4; j++)
                {
                    tmpRow.GetCell(j).CellStyle = styleCenter;
                }
            }

            row0.CreateCell(6).SetCellValue("统计类别");
            row0.CreateCell(7).SetCellValue("分子");
            row0.CreateCell(8).SetCellValue("分母");
            row0.CreateCell(9).SetCellValue("比率");
            row0.CreateCell(10).SetCellValue("备注");
            for (int i = 6; i <= 10; i++)
            {
                row0.GetCell(i).CellStyle = styleCenter;
            }
            for (int i = 0; i < passRates.Count; i++)
            {
                var tmpRow = sheet.GetRow(i + 1);
                tmpRow.Height = 30 * 20;
                tmpRow.CreateCell(6).SetCellValue(passRates[i].Category);
                tmpRow.CreateCell(7).SetCellValue(passRates[i].Molecular);
                tmpRow.CreateCell(8).SetCellValue(passRates[i].Denominator);
                tmpRow.CreateCell(9).SetCellValue((double)passRates[i].Rate);
                tmpRow.CreateCell(10).SetCellValue(passRates[i].Remark);

                for (int j = 6; j <= 10; j++)
                {
                    tmpRow.GetCell(j).CellStyle = styleCenter;
                }
            }
            var titleRow = sheet.GetRow(passRates.Count + 2);
            titleRow.Height = 30 * 20;
            titleRow.CreateCell(6).SetCellValue("IV分布统计");
            for (int i = 0; i < ivDistributes.Count; i++)
            {
                var tmpRow = sheet.GetRow(passRates.Count + 3 + i);
                tmpRow.Height = 30 * 20;
                tmpRow.CreateCell(6).SetCellValue(ivDistributes[i].Category);
                tmpRow.CreateCell(7).SetCellValue(ivDistributes[i].Molecular);
                tmpRow.CreateCell(8).SetCellValue(ivDistributes[i].Denominator);
                tmpRow.CreateCell(9).SetCellValue((double)ivDistributes[i].Rate);
                tmpRow.CreateCell(10).SetCellValue(ivDistributes[i].Remark);

                for (int j = 6; j <= 10; j++)
                {
                    tmpRow.GetCell(j).CellStyle = styleCenter;
                }
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"生产报表（{workshop.WorkshopName}：{startTime:yyyyMMddHHmm}-{endTime:yyyyMMddHHmm}）"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
            return;
        }
        #endregion

        #region 图片下载
        public ActionResult DownloadImages()
        {
            return View();
        }

        /// <summary>
        /// 图片下载
        /// </summary>
        [HttpGet]
        public FileResult DownloadImageZip(int byType, string code, string siteTypes)
        {
            return Download(byType, code, siteTypes);
        }
        [HttpPost]
        public FileResult PostDownloadImageZip(int byType, string code, string siteTypes)
        {
            return Download(byType, code, siteTypes);
        }
        private FileResult Download(int byType, string code, string siteTypes)
        {
            if (byType < 0 || byType > 2)
            {
                return File(Encoding.UTF8.GetBytes("下载类型异常"), "text/html; charset=utf-8");
            }
            else if (string.IsNullOrWhiteSpace(code))
            {
                return File(Encoding.UTF8.GetBytes("请输入条码"), "text/html; charset=utf-8");
            }
            var sites = siteTypes.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(_ => Convert.ToInt32(_)).ToList();
            if (!sites.Any())
            {
                return File(Encoding.UTF8.GetBytes("请选择下载图片类型"), "text/html; charset=utf-8");
            }

            var components = new List<VComponent>();
            switch (byType)
            {
                case 0:
                    components.AddRange(new ComponentMgr().QueryComponents(string.Empty, string.Empty, code.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList()));
                    break;
                case 1:
                    components.AddRange(new ComponentMgr().QueryComponents(string.Empty, code, new List<string>()));
                    break;
                case 2:
                    components.AddRange(new ComponentMgr().QueryComponents(code, string.Empty, new List<string>()));
                    break;
                default:
                    break;
            }
            if (!components.Any())
            {
                return File(Encoding.UTF8.GetBytes("没有可以下载的组件"), "text/html; charset=utf-8");
            }

            var images = new List<KeyValuePair<string, List<KeyValuePair<string, Stream>>>>();
            var ftp = new FtpUtils(Convert.ToString(ConfigurationManager.AppSettings["FtpUserName"]), Convert.ToString(ConfigurationManager.AppSettings["FtpPassword"]));
            foreach (var site in sites)
            {
                var siteName = string.Empty;
                switch (site)
                {
                    case 1:
                        siteName = "EL1图片";
                        break;
                    case 2:
                        siteName = "接线盒图片";
                        break;
                    case 3:
                        siteName = "IV数据";
                        break;
                    case 4:
                        siteName = "IV图片";
                        break;
                    case 5:
                        siteName = "EL2图片";
                        break;
                }
                var siteKeyValuePair = new KeyValuePair<string, List<KeyValuePair<string, Stream>>>(siteName, new List<KeyValuePair<string, Stream>>());

                //下载图片
                if (site != 3)
                {
                    var pageSize = 10;
                    var pageCount = (components.Count % pageSize == 0) ? components.Count / pageSize : components.Count / pageSize + 1;
                    for (var page = 0; page < pageCount; page++)
                    {
                        var tasks = new List<Task>();
                        for (var i = 0; i < pageSize; i++)
                        {
                            var curIndex = page * pageSize + i;
                            if (curIndex >= components.Count)
                            {
                                break;
                            }
                            var component = components[curIndex];
                            var url = string.Empty;
                            switch (site)
                            {
                                case 1:
                                    url = component.EL1Image;
                                    break;
                                case 2:
                                    url = component.BoxImage;
                                    break;
                                case 4:
                                    url = component.IvImage;
                                    break;
                                case 5:
                                    url = component.EL2Image;
                                    break;
                            }
                            if (string.IsNullOrWhiteSpace(url))
                            {
                                continue;
                            }
                            var task = new Task(() =>
                            {
                                try
                                {
                                    var buffer = ftp.Download(url);
                                    siteKeyValuePair.Value.Add(new KeyValuePair<string, Stream>(component.BarCode, new MemoryStream(buffer)));
                                }
                                catch (Exception ex)
                                {
                                    logger.Fatal(ex, $"下载图片异常：{url}");
                                }
                            });
                            task.Start();
                            tasks.Add(task);
                        }
                        Task.WaitAll(tasks.ToArray());
                    }
                }
                else
                {
                    var ivDatas = new ProductViaSiteMgr().QueryIvDatas(components.Select(_ => _.BarCode).ToList());
                    siteKeyValuePair.Value.Add(new KeyValuePair<string, Stream>("IV数据", ComopressIvData(ivDatas)));
                }
                images.Add(siteKeyValuePair);
            }

            var bytes = Compress(images);

            return File(bytes, "application/zip", (byType == 0 ? "组件图片" : code) + $"_{DateTime.Now:yyMMddHHmmss}.zip");
        }
        private Stream ComopressIvData(List<VProductViaSiteIVData> ivDatas)
        {
            var book = new XSSFWorkbook();
            var sheet = book.CreateSheet("IV数据");
            var stream = new NpoiMemoryStream();
            stream.AllowClose = false;
            if (!ivDatas.Any())
            {
                book.Write(stream);
                book = null;
                return stream;
            }

            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            var fstData = JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(ivDatas.FirstOrDefault().FullData);
            var row1 = sheet.CreateRow(0);
            row1.Height = 30 * 20;
            row1.CreateCell(0).SetCellValue("序号");
            row1.GetCell(0).CellStyle = styleCenter;
            foreach (var item in fstData)
            {
                var cell = row1.CreateCell(fstData.IndexOf(item) + 1);
                cell.SetCellValue(item.Key);
                cell.CellStyle = styleCenter;
            }

            var ivGroups = ivDatas.GroupBy(_ => _.BarCode);
            var i = 1;
            foreach (var item in ivGroups)
            {
                var tmpRow = sheet.CreateRow(i);
                tmpRow.Height = 30 * 20;
                tmpRow.CreateCell(0).SetCellValue(i);
                tmpRow.GetCell(0).CellStyle = styleCenter;

                var curData = JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(item.OrderByDescending(_ => _.PkId).FirstOrDefault().FullData);
                foreach (var data in curData)
                {
                    var cell = tmpRow.CreateCell(curData.IndexOf(data) + 1);
                    cell.SetCellValue(data.Value);
                    cell.CellStyle = styleCenter;
                }
                i++;
            }

            book.Write(stream);
            book = null;
            return stream;
        }
        private byte[] Compress(List<KeyValuePair<string, List<KeyValuePair<string, Stream>>>> fileStreams)
        {
            var url = string.Empty;
            byte[] bytes;
            using (MemoryStream st = new MemoryStream())
            {
                using (ZipOutputStream zipStream = new ZipOutputStream(st))
                {
                    foreach (var site in fileStreams)
                    {
                        var folderEntry = new ZipEntry($"{site.Key.Replace("/", "")}/");
                        folderEntry.IsUnicodeText = true;
                        zipStream.PutNextEntry(folderEntry);
                        foreach (var item in site.Value)
                        {
                            var ext = StringUtils.Equals(site.Key, "IV数据") ? "xlsx" : "jpg";
                            var zipEntry = new ZipEntry($"{site.Key}/{item.Key}.{ext}");
                            zipEntry.DateTime = DateTime.Now;
                            zipEntry.IsUnicodeText = true;
                            zipStream.PutNextEntry(zipEntry);
                            var buffer = new byte[item.Value.Length];
                            item.Value.Seek(0, SeekOrigin.Begin);
                            item.Value.Read(buffer, 0, buffer.Length);
                            zipStream.Write(buffer, 0, buffer.Length);
                            zipStream.CloseEntry();
                        }
                    }

                    zipStream.IsStreamOwner = false;
                    zipStream.Finish();
                    zipStream.Close();
                }
                st.Seek(0, SeekOrigin.Begin);
                bytes = new byte[st.Length];
                st.Read(bytes, 0, bytes.Length);
                st.Close();
            }
            return bytes;
        }
        #endregion

        #region 实时产能
        public ActionResult RealTimeCapacity()
        {
            var siteTypes = new CommonMgr().QueryEnumTypes(typeof(SiteType)).Where(_ => _.Key != (int)SiteType.Sort).ToList();
            siteTypes.Insert(0, new KeyValuePair<int, string>(0, "分片"));
            ViewBag.SiteTypes = siteTypes;
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 查询实时产能
        /// </summary>
        [HttpPost]
        public JsonResult GetRealTimeCapacity(string workshopCode, DateTime startTime, DateTime endTime)
        {
            var report = new ReportMgr().GetRealTimeCapacity(workshopCode, startTime, endTime);
            return Json(new ResultJson<object>(report));
        }

        /// <summary>
        /// 查询小时产能
        /// </summary>
        [HttpPost]
        public JsonResult GetHourCapacity(string workshopCode, int siteType, DateTime startTime, DateTime endTime)
        {
            var report = new ReportMgr().GetHourCapacity(workshopCode, siteType, startTime, endTime);
            return Json(new ResultJson<object>(report));
        }
        #endregion

        #region 质量报表
        public ActionResult QualityReport()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 查询质量报表数据
        /// </summary>
        [HttpPost]
        public JsonResult QueryQualityReports(int page, int limit, string workshopCode, int lineId, DateTime startTime, DateTime endTime)
        {
            var total = 0;
            var report = new ReportMgr().QueryQualityReports(page, limit, out total, workshopCode, lineId, "", "", "", "", "", startTime, endTime);
            return Json(new ResultJson<object>(report, total));
        }

        /// <summary>
        /// 导出质量报表数据
        /// </summary>
        public void ExportQualityReports(string workshopCode, int lineId, DateTime startTime, DateTime endTime)
        {
            var total = 0;
            var report = new ReportMgr().QueryQualityReports(1, 0, out total, workshopCode, lineId, "", "", "", "", "", startTime, endTime);
            var workshop = new WorkshopMgr().GetWorkshop(workshopCode);

            var book = new XSSFWorkbook();
            var sheet = book.CreateSheet("质量报表");
            for (int i = 0; i <= 31; i++)
            {
                sheet.SetColumnWidth(i, 8 * 256 + 200);
            }
            sheet.SetColumnWidth(1, 8 * 800 + 200);
            sheet.SetColumnWidth(15, 8 * 400 + 200);
            sheet.SetColumnWidth(16, 8 * 800 + 200);

            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            var row1 = sheet.CreateRow(0);
            row1.Height = 30 * 20;
            row1.CreateCell(0).SetCellValue("ID");
            row1.CreateCell(1).SetCellValue("条码");
            row1.CreateCell(2).SetCellValue("电池厂商");
            row1.CreateCell(3).SetCellValue("效率");
            row1.CreateCell(4).SetCellValue("单片功率");
            row1.CreateCell(5).SetCellValue("理论功率");
            row1.CreateCell(6).SetCellValue("Voc");
            row1.CreateCell(7).SetCellValue("Isc");
            row1.CreateCell(8).SetCellValue("Vm");
            row1.CreateCell(9).SetCellValue("Im");
            row1.CreateCell(10).SetCellValue("FF");
            row1.CreateCell(11).SetCellValue("Pmax");
            row1.CreateCell(12).SetCellValue("CTM");
            row1.CreateCell(13).SetCellValue("额定功率");
            row1.CreateCell(14).SetCellValue("订单号");
            row1.CreateCell(15).SetCellValue("工单号");
            row1.CreateCell(16).SetCellValue("电池等级");
            row1.CreateCell(17).SetCellValue("电池型号");
            row1.CreateCell(18).SetCellValue("电池颜色");
            row1.CreateCell(19).SetCellValue("焊接机");
            row1.CreateCell(20).SetCellValue("电池片数");
            row1.CreateCell(21).SetCellValue("方案");
            row1.CreateCell(22).SetCellValue("温度");
            row1.CreateCell(23).SetCellValue("转换功率");
            row1.CreateCell(24).SetCellValue("光强");
            row1.CreateCell(25).SetCellValue("串阻");
            row1.CreateCell(26).SetCellValue("并阻");
            row1.CreateCell(27).SetCellValue("封装损耗");
            row1.CreateCell(28).SetCellValue("档位");
            row1.CreateCell(29).SetCellValue("打包编号");
            row1.CreateCell(30).SetCellValue("打包时间");
            row1.CreateCell(31).SetCellValue("操作员");
            row1.CreateCell(32).SetCellValue("班次");
            row1.CreateCell(33).SetCellValue("时间");
            for (int i = 0; i <= 33; i++)
            {
                row1.GetCell(i).CellStyle = styleCenter;
            }

            if (!report.Any())
            {
                var ms1 = new MemoryStream();
                book.Write(ms1);
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"质量报表（{workshop.WorkshopName}：{startTime:yyyyMMddHHmm}-{endTime:yyyyMMddHHmm}）"));
                Response.BinaryWrite(ms1.ToArray());
                book = null;
                ms1.Close();
                ms1.Dispose();
                return;
            }
            for (int i = 0; i < report.Count; i++)
            {
                var tmpRow = sheet.CreateRow(i + 1);
                tmpRow.Height = 30 * 20;
                tmpRow.CreateCell(0).SetCellValue(i + 1);
                tmpRow.CreateCell(1).SetCellValue(report[i].BarCode);
                tmpRow.CreateCell(2).SetCellValue(report[i].CellCompany);
                tmpRow.CreateCell(3).SetCellValue((double)report[i].Efficiency);
                tmpRow.CreateCell(4).SetCellValue((double)report[i].SinglePower);
                tmpRow.CreateCell(5).SetCellValue((double)report[i].TheoreticalPower);
                tmpRow.CreateCell(6).SetCellValue((double)report[i].Voc);
                tmpRow.CreateCell(7).SetCellValue((double)report[i].Isc);
                tmpRow.CreateCell(8).SetCellValue((double)report[i].Vm);
                tmpRow.CreateCell(9).SetCellValue((double)report[i].Im);
                tmpRow.CreateCell(10).SetCellValue((double)report[i].FF);
                tmpRow.CreateCell(11).SetCellValue((double)report[i].Pmax);
                tmpRow.CreateCell(12).SetCellValue((double)report[i].CTM);
                tmpRow.CreateCell(13).SetCellValue((double)report[i].RatedPower);
                tmpRow.CreateCell(14).SetCellValue(report[i].OrderNo ?? string.Empty);
                tmpRow.CreateCell(15).SetCellValue(report[i].WorkOrderNo ?? string.Empty);
                tmpRow.CreateCell(16).SetCellValue(report[i].CellLevel ?? string.Empty);
                tmpRow.CreateCell(17).SetCellValue(report[i].CellModel ?? string.Empty);
                tmpRow.CreateCell(18).SetCellValue(report[i].CellColor ?? string.Empty);
                tmpRow.CreateCell(19).SetCellValue(report[i].WeldingMachine ?? string.Empty);
                tmpRow.CreateCell(20).SetCellValue(report[i].CellPiece);
                tmpRow.CreateCell(21).SetCellValue(report[i].Schema ?? string.Empty);
                tmpRow.CreateCell(22).SetCellValue((double)report[i].Temp);
                tmpRow.CreateCell(23).SetCellValue((double)report[i].ConversionEfficiency);
                tmpRow.CreateCell(24).SetCellValue((double)report[i].LightIntensity);
                tmpRow.CreateCell(25).SetCellValue((double)report[i].SeriesResistance);
                tmpRow.CreateCell(26).SetCellValue((double)report[i].ParallelResistance);
                tmpRow.CreateCell(27).SetCellValue((double)report[i].PackagingLoss);
                tmpRow.CreateCell(28).SetCellValue(report[i].CurrentLevel ?? string.Empty);
                tmpRow.CreateCell(29).SetCellValue(report[i].PackNo ?? string.Empty);
                tmpRow.CreateCell(30).SetCellValue(report[i].PackTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? string.Empty);
                tmpRow.CreateCell(31).SetCellValue(report[i].Operator ?? string.Empty);
                tmpRow.CreateCell(32).SetCellValue(report[i].Duty ?? string.Empty);
                tmpRow.CreateCell(33).SetCellValue(report[i].Time.ToString("yyyy-MM-dd HH:mm:ss"));
                for (int j = 0; j <= 33; j++)
                {
                    tmpRow.GetCell(j).CellStyle = styleCenter;
                }
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"质量报表（{workshop.WorkshopName}：{startTime:yyyyMMddHHmm}-{endTime:yyyyMMddHHmm}）"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
            return;
        }
        #endregion

        #region 客户报表
        /// <summary>
        /// 隆基报表
        /// </summary>
        public ActionResult LongiReport()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 隆基报表
        /// </summary>
        public JsonResult QueryLongiReports(int page, int limit, string workshopCode, int lineId, string orderNo, string workOrderNo, string cabinetNo, string packNo, string barCode, DateTime? startTime, DateTime? endTime)
        {
            var total = 0;
            var report = new ReportMgr().QueryQualityReports(page, limit, out total, workshopCode, lineId, orderNo, workOrderNo, cabinetNo, packNo, barCode, startTime, endTime);
            return Json(new ResultJson<object>(report, total));
        }

        /// <summary>
        /// 导出隆基报表
        /// </summary>
        public void ExportLongiReports(string workshopCode, int lineId, string orderNo, string workOrderNo, string cabinetNo, string packNo, string barCode, DateTime startTime, DateTime endTime)
        {
            var total = 0;
            var report = new ReportMgr().QueryQualityReports(1, 0, out total, workshopCode, lineId, orderNo, workOrderNo, cabinetNo, packNo, barCode, startTime, endTime)
                .OrderBy(_ => _.CabinetNo).ThenBy(_ => _.PackNo).ThenBy(_ => _.BarCode).ToList();
            var workshop = new WorkshopMgr().GetWorkshop(workshopCode);

            var book = new XSSFWorkbook();
            var sheet = book.CreateSheet("晶飞报表");
            for (int i = 0; i < 33; i++)
            {
                sheet.SetColumnWidth(i, 8 * 256 + 200);
            }
            sheet.SetColumnWidth(0, 8 * 800 + 200);
            sheet.SetColumnWidth(7, 8 * 400 + 200);
            sheet.SetColumnWidth(9, 8 * 400 + 200);
            sheet.SetColumnWidth(10, 8 * 400 + 200);
            sheet.SetColumnWidth(14, 8 * 400 + 200);
            sheet.SetColumnWidth(15, 8 * 400 + 200);
            sheet.SetColumnWidth(18, 8 * 400 + 200);
            sheet.SetColumnWidth(19, 8 * 500 + 200);
            sheet.SetColumnWidth(20, 8 * 300 + 200);
            sheet.SetColumnWidth(23, 8 * 300 + 200);
            sheet.SetColumnWidth(234, 8 * 300 + 200);

            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            var row1 = sheet.CreateRow(0);
            row1.Height = 30 * 20;
            row1.CreateCell(0).SetCellValue("Module ID");
            row1.CreateCell(1).SetCellValue("Voc(V)");
            row1.CreateCell(2).SetCellValue("Isc(A)");
            row1.CreateCell(3).SetCellValue("Vpm(V)");
            row1.CreateCell(4).SetCellValue("Ipm(A)");
            row1.CreateCell(5).SetCellValue("Pm(W)");
            row1.CreateCell(6).SetCellValue("FF(%)");
            row1.CreateCell(7).SetCellValue("Marking(W)");
            row1.CreateCell(8).SetCellValue("效率");
            row1.CreateCell(9).SetCellValue("Pallet ID");
            row1.CreateCell(10).SetCellValue("Container No");
            row1.CreateCell(11).SetCellValue("Current LV（H/M/L）");
            row1.CreateCell(12).SetCellValue("eff");
            row1.CreateCell(13).SetCellValue("Rs");
            row1.CreateCell(14).SetCellValue("Rsh");
            row1.CreateCell(15).SetCellValue("Module Type");
            row1.CreateCell(16).SetCellValue("Quality Grade");
            row1.CreateCell(17).SetCellValue("Module color");
            row1.CreateCell(18).SetCellValue("Back");
            row1.CreateCell(19).SetCellValue("Aluminum");
            row1.CreateCell(20).SetCellValue("Junction Box");
            row1.CreateCell(21).SetCellValue("order no");
            row1.CreateCell(22).SetCellValue("work remark");
            row1.CreateCell(23).SetCellValue("电池厂家");
            row1.CreateCell(24).SetCellValue("测试时间");
            row1.CreateCell(25).SetCellValue("包装时间");
            row1.CreateCell(26).SetCellValue("包装方式");
            row1.CreateCell(27).SetCellValue("电池片");
            row1.CreateCell(28).SetCellValue("article no");
            row1.CreateCell(29).SetCellValue("Item Number");
            row1.CreateCell(30).SetCellValue("电池片批次号");
            for (int i = 0; i <= 30; i++)
            {
                row1.GetCell(i).CellStyle = styleCenter;
            }

            if (!report.Any())
            {
                var ms1 = new MemoryStream();
                book.Write(ms1);
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"晶飞报表（{workshop.WorkshopName}：{startTime:yyyyMMddHHmm}-{endTime:yyyyMMddHHmm}）"));
                Response.BinaryWrite(ms1.ToArray());
                book = null;
                ms1.Close();
                ms1.Dispose();
                return;
            }
            for (int i = 0; i < report.Count; i++)
            {
                var tmpRow = sheet.CreateRow(i + 1);
                tmpRow.Height = 30 * 20;
                tmpRow.CreateCell(0).SetCellValue(report[i].BarCode);
                tmpRow.CreateCell(1).SetCellValue((double)report[i].Voc);
                tmpRow.CreateCell(2).SetCellValue((double)report[i].Isc);
                tmpRow.CreateCell(3).SetCellValue((double)report[i].Vm);
                tmpRow.CreateCell(4).SetCellValue((double)report[i].Im);
                tmpRow.CreateCell(5).SetCellValue((double)report[i].Pmax);
                tmpRow.CreateCell(6).SetCellValue((double)report[i].FF);
                tmpRow.CreateCell(7).SetCellValue((double)report[i].RatedPower);
                tmpRow.CreateCell(8).SetCellValue((double)report[i].Efficiency);
                tmpRow.CreateCell(9).SetCellValue(report[i].PackNo);
                tmpRow.CreateCell(10).SetCellValue(report[i].CabinetNo);
                tmpRow.CreateCell(11).SetCellValue(report[i].CurrentLevel);
                tmpRow.CreateCell(12).SetCellValue((double)report[i].ModEff);
                tmpRow.CreateCell(13).SetCellValue((double)report[i].Ress);
                tmpRow.CreateCell(14).SetCellValue((double)report[i].Ressh);
                tmpRow.CreateCell(15).SetCellValue(report[i].NameplateModel);
                tmpRow.CreateCell(16).SetCellValue(report[i].QLevel ?? string.Empty);
                tmpRow.CreateCell(17).SetCellValue(report[i].ComponentColor ?? string.Empty);
                tmpRow.CreateCell(18).SetCellValue(report[i].BackPlanSpecs ?? string.Empty);
                tmpRow.CreateCell(19).SetCellValue(report[i].AluFrameSpecs ?? string.Empty);
                tmpRow.CreateCell(20).SetCellValue(report[i].JunBoxSpecs ?? string.Empty);
                tmpRow.CreateCell(21).SetCellValue(report[i].OrderNo ?? string.Empty);
                tmpRow.CreateCell(22).SetCellValue(report[i].WorkRemark);
                tmpRow.CreateCell(23).SetCellValue(report[i].BatteryFactory ?? string.Empty);
                tmpRow.CreateCell(24).SetCellValue(report[i].IVTime?.ToString("yyyy-MM-dd"));
                tmpRow.CreateCell(25).SetCellValue(report[i].PackTime?.ToString("yyyy-MM-dd"));
                tmpRow.CreateCell(26).SetCellValue((double)report[i].LightIntensity);
                tmpRow.CreateCell(27).SetCellValue(report[i].BatterySpecs);
                tmpRow.CreateCell(28).SetCellValue(report[i].ArticleNo);
                tmpRow.CreateCell(29).SetCellValue(report[i].ItemNumber);
                tmpRow.CreateCell(30).SetCellValue(report[i].BatteryBatchNo);
                for (int j = 0; j <= 30; j++)
                {
                    tmpRow.GetCell(j).CellStyle = styleCenter;
                }
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"晶飞报表（{workshop.WorkshopName}：{startTime:yyyyMMddHHmm}-{endTime:yyyyMMddHHmm}）"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
            return;
        }
        #endregion

        #region 漏扫统计
        /// <summary>
        /// 漏扫记录
        /// </summary>
        public ViewResult MissingViaSite()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 查询漏扫记录
        /// </summary>
        [HttpPost]
        public JsonResult QueryMissingViaSiteComponents(string workshopCode, DateTime startTime, DateTime endTime)
        {
            var report = new ReportMgr().QueryMissingViaSiteCounts(workshopCode, startTime, endTime);
            return Json(new ResultJson<object>(new
            {
                Report = report,
                SiteTypes = new CommonMgr().QueryEnumTypes(typeof(SiteType)).Select(_ => _.Value).ToList()
            }));
        }

        /// <summary>
        /// 查询漏扫记录
        /// </summary>
        public void ExportMissingViaSiteComponents(string workshopCode, DateTime startTime, DateTime endTime)
        {
            var report = new ReportMgr().QueryMissingViaSiteComponents(workshopCode, startTime, endTime);
            var book = new XSSFWorkbook();

            foreach (var item in report)
            {
                var sheet = book.CreateSheet(item.Key.Replace("/", ""));
                for (int i = 0; i < 2; i++)
                {
                    sheet.SetColumnWidth(i, 8 * 400 + 200);
                }

                var styleCenter = book.CreateCellStyle();
                styleCenter.Alignment = HorizontalAlignment.Center;
                styleCenter.VerticalAlignment = VerticalAlignment.Center;
                styleCenter.BorderBottom = BorderStyle.Thin;
                styleCenter.BorderLeft = BorderStyle.Thin;
                styleCenter.BorderRight = BorderStyle.Thin;
                styleCenter.BorderTop = BorderStyle.Thin;

                var row1 = sheet.CreateRow(0);
                row1.Height = 30 * 20;
                row1.CreateCell(0).SetCellValue("序号");
                row1.CreateCell(1).SetCellValue("条码");
                for (int i = 0; i < 2; i++)
                {
                    row1.GetCell(i).CellStyle = styleCenter;
                }
                for (int i = 0; i < item.Value.Count; i++)
                {
                    var tmpRow = sheet.CreateRow(i + 1);
                    tmpRow.Height = 30 * 20;
                    tmpRow.CreateCell(0).SetCellValue(i + 1);
                    tmpRow.CreateCell(1).SetCellValue(item.Value[i]);
                    for (int j = 0; j < 2; j++)
                    {
                        tmpRow.GetCell(j).CellStyle = styleCenter;
                    }
                }
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"漏扫记录（{startTime:yyyyMMddHHmm}-{endTime:yyyyMMddHHmm}）"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
        }
        #endregion

        #region 过站分析
        /// <summary>
        /// 过站分析
        /// </summary>
        public ActionResult ViaSiteAnalysis()
        {
            var sites = new List<string> { "分片" };
            sites.AddRange(new CommonMgr().QueryEnumTypes(typeof(SiteType)).Where(_ => _.Key != (int)SiteType.Sort).Select(_ => _.Value));
            sites.Add("入库");
            ViewBag.Sites = sites;
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 查询过站分析
        /// </summary>
        [HttpPost]
        public JsonResult QueryViaSiteAnalysis(string workshopCode, string orderNo, string workOrderNo, int lineId, DateTime? startTime, DateTime? endTime)
        {
            var report = new ReportMgr().QueryViaSiteAnalysis(workshopCode, orderNo, workOrderNo, lineId, startTime, endTime);
            return Json(new ResultJson<object>(report.Select(_ => new KeyValuePair<string, int>(_.Key, _.Value.Count)).ToList()));
        }

        /// <summary>
        /// 查询过站分析
        /// </summary>
        public void BatchExportViaSiteAnalysis(string barCodes)
        {
            var report = new ReportMgr().QueryViaSiteAnalysis(barCodes.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList());
            ExportViaSiteAnalysisExcel(report);
        }

        /// <summary>
        /// 查询过站分析
        /// </summary>
        public void ExportViaSiteAnalysis(string workshopCode, string orderNo, string workOrderNo, int lineId, DateTime? startTime, DateTime? endTime)
        {
            var report = new ReportMgr().QueryViaSiteAnalysis(workshopCode, orderNo, workOrderNo, lineId, startTime, endTime);
            ExportViaSiteAnalysisExcel(report);
        }
        private void ExportViaSiteAnalysisExcel(List<KeyValuePair<string, List<VComponent>>> report)
        {
            var book = new XSSFWorkbook();

            var sheet = book.CreateSheet("过站组件");
            for (int i = 0; i <= 48; i++)
            {
                sheet.SetColumnWidth(i, 20 * 256 + 200);
            }
            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            var rowHeader = sheet.CreateRow(0);
            rowHeader.Height = 30 * 20;
            rowHeader.CreateCell(0).SetCellValue("序号");
            rowHeader.CreateCell(1).SetCellValue("组件条码");
            rowHeader.CreateCell(2).SetCellValue("订单号");
            rowHeader.CreateCell(3).SetCellValue("工单号");
            rowHeader.CreateCell(4).SetCellValue("成品料号");
            rowHeader.CreateCell(5).SetCellValue("成品名称");
            rowHeader.CreateCell(6).SetCellValue("生产车间");
            rowHeader.CreateCell(7).SetCellValue("最终站点");
            rowHeader.CreateCell(8).SetCellValue("分片时间");
            rowHeader.CreateCell(9).SetCellValue("焊接时间");
            rowHeader.CreateCell(10).SetCellValue("焊接站点");
            rowHeader.CreateCell(11).SetCellValue("焊接操作员");
            rowHeader.CreateCell(12).SetCellValue("叠层时间");
            rowHeader.CreateCell(13).SetCellValue("叠层站点");
            rowHeader.CreateCell(14).SetCellValue("叠层操作员");
            rowHeader.CreateCell(15).SetCellValue("EL1时间");
            rowHeader.CreateCell(16).SetCellValue("EL1站点");
            rowHeader.CreateCell(17).SetCellValue("EL1操作员");
            rowHeader.CreateCell(18).SetCellValue("层压时间");
            rowHeader.CreateCell(19).SetCellValue("层压站点");
            rowHeader.CreateCell(20).SetCellValue("层压位置");
            rowHeader.CreateCell(21).SetCellValue("层压操作员");
            rowHeader.CreateCell(22).SetCellValue("NQC时间");
            rowHeader.CreateCell(23).SetCellValue("NQC站点");
            rowHeader.CreateCell(24).SetCellValue("NQC操作员");
            rowHeader.CreateCell(25).SetCellValue("装框时间");
            rowHeader.CreateCell(26).SetCellValue("装框站点");
            rowHeader.CreateCell(27).SetCellValue("装框操作员");
            rowHeader.CreateCell(28).SetCellValue("清洗时间");
            rowHeader.CreateCell(29).SetCellValue("清洗站点");
            rowHeader.CreateCell(30).SetCellValue("清洗操作员");
            rowHeader.CreateCell(31).SetCellValue("IV测试时间");
            rowHeader.CreateCell(32).SetCellValue("IV测试站点");
            rowHeader.CreateCell(33).SetCellValue("IV测试操作员");
            rowHeader.CreateCell(34).SetCellValue("绝缘耐压时间");
            rowHeader.CreateCell(35).SetCellValue("绝缘耐压站点");
            rowHeader.CreateCell(36).SetCellValue("绝缘耐压操作员");
            rowHeader.CreateCell(37).SetCellValue("EL2时间");
            rowHeader.CreateCell(38).SetCellValue("EL2站点");
            rowHeader.CreateCell(39).SetCellValue("EL2操作员");
            rowHeader.CreateCell(40).SetCellValue("铭牌打印时间");
            rowHeader.CreateCell(41).SetCellValue("铭牌打印站点");
            rowHeader.CreateCell(42).SetCellValue("铭牌打印操作员");
            rowHeader.CreateCell(43).SetCellValue("终检时间");
            rowHeader.CreateCell(44).SetCellValue("终检站点");
            rowHeader.CreateCell(45).SetCellValue("终检操作员");
            rowHeader.CreateCell(46).SetCellValue("包装时间");
            rowHeader.CreateCell(47).SetCellValue("包装站点");
            rowHeader.CreateCell(48).SetCellValue("包装操作员");
            rowHeader.CreateCell(49).SetCellValue("入库时间");
            var rowIndex = 1;
            foreach (var item in report.FirstOrDefault(_ => _.Key == "分片").Value)
            {
                var tmpRow = sheet.CreateRow(rowIndex);
                tmpRow.CreateCell(0).SetCellValue(rowIndex);
                tmpRow.CreateCell(1).SetCellValue(item.BarCode);
                tmpRow.CreateCell(2).SetCellValue(item.OrderNo);
                tmpRow.CreateCell(3).SetCellValue(item.WorkOrderNo);
                tmpRow.CreateCell(4).SetCellValue(item.ProductCode);
                tmpRow.CreateCell(5).SetCellValue(item.ProductName);
                tmpRow.CreateCell(6).SetCellValue(item.WorkshopName);
                tmpRow.CreateCell(7).SetCellValue(!string.IsNullOrWhiteSpace(item.CNLastSite) ? item.CNLastSite : (item.DistributeTime.HasValue ? "分片" : ""));
                tmpRow.CreateCell(8).SetCellValue(item.DistributeTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(9).SetCellValue(item.WeldingTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(10).SetCellValue(item.WeldingMachine);
                tmpRow.CreateCell(11).SetCellValue(item.WeldingUser);
                tmpRow.CreateCell(12).SetCellValue(item.LaminatedTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(13).SetCellValue(item.LaminatedMachine);
                tmpRow.CreateCell(14).SetCellValue(item.LaminatedUser);
                tmpRow.CreateCell(15).SetCellValue(item.EL1Time?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(16).SetCellValue(item.EL1Machine);
                tmpRow.CreateCell(17).SetCellValue(item.EL1User);
                tmpRow.CreateCell(18).SetCellValue(item.TopTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(19).SetCellValue(item.TopMachine);
                tmpRow.CreateCell(20).SetCellValue(item.TopPosition);
                tmpRow.CreateCell(21).SetCellValue(item.TopUser);
                tmpRow.CreateCell(22).SetCellValue(item.NQCTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(23).SetCellValue(item.NQCMachine);
                tmpRow.CreateCell(24).SetCellValue(item.NQCUser);
                tmpRow.CreateCell(25).SetCellValue(item.FramingTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(26).SetCellValue(item.FramingMachine);
                tmpRow.CreateCell(27).SetCellValue(item.FramingUser);
                tmpRow.CreateCell(28).SetCellValue(item.CleanTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(29).SetCellValue(item.CleanMachine);
                tmpRow.CreateCell(30).SetCellValue(item.CleanUser);
                tmpRow.CreateCell(31).SetCellValue(item.IVTestTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(32).SetCellValue(item.IVTestMachine);
                tmpRow.CreateCell(33).SetCellValue(item.IVTestUser);
                tmpRow.CreateCell(34).SetCellValue(item.InsulatedResistanceTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(35).SetCellValue(item.InsulatedResistanceMachine);
                tmpRow.CreateCell(36).SetCellValue(item.InsulatedResistanceUser);
                tmpRow.CreateCell(37).SetCellValue(item.EL2Time?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(38).SetCellValue(item.EL2Machine);
                tmpRow.CreateCell(39).SetCellValue(item.EL2User);
                tmpRow.CreateCell(40).SetCellValue(item.NameplatePrintingTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(41).SetCellValue(item.NameplatePrintingMachine);
                tmpRow.CreateCell(42).SetCellValue(item.NameplatePrintingUser);
                tmpRow.CreateCell(43).SetCellValue(item.FinalInspectionTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(44).SetCellValue(item.FinalInspectionMachine);
                tmpRow.CreateCell(45).SetCellValue(item.FinalInspectionUser);
                tmpRow.CreateCell(46).SetCellValue(item.PackTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(47).SetCellValue(item.PackMachine);
                tmpRow.CreateCell(48).SetCellValue(item.PackUser);
                tmpRow.CreateCell(49).SetCellValue(item.InboundTime?.ToString("yyyy/MM/dd HH:mm:ss"));

                for (int i = 0; i <= 48; i++)
                {
                    tmpRow.GetCell(i).CellStyle = styleCenter;
                }
                rowIndex++;
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"过站组件"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
        }

        /// <summary>
        /// 过站分析详情
        /// </summary>
        public ActionResult ViaSiteAnalysisDetail(string siteType, string workshopCode, string orderNo, string workOrderNo, DateTime? startTime, DateTime? endTime)
        {
            return View();
        }

        /// <summary>
        /// 查询过站分析详情列表
        /// </summary>
        [HttpPost]
        public JsonResult QueryViaSiteAnalyseDetails(int page, int limit, string siteType, bool hasViaSite, string workshopCode, string orderNo, string workOrderNo, int lineId, DateTime? startTime, DateTime? endTime)
        {
            var total = 0;
            var result = new ReportMgr().QueryViaSiteAnalyseDetails(page, limit, out total, siteType, hasViaSite, workshopCode, orderNo, workOrderNo, lineId, startTime, endTime);
            return Json(new ResultJson<object>(result, total));
        }

        /// <summary>
        /// 查询过站分析详情列表
        /// </summary>
        public void ExportViaSiteAnalyseDetails(string siteType, bool hasViaSite, string workshopCode, string orderNo, string workOrderNo, int lineId, DateTime? startTime, DateTime? endTime)
        {
            var total = 0;
            var result = new ReportMgr().QueryViaSiteAnalyseDetails(1, 0, out total, siteType, hasViaSite, workshopCode, orderNo, workOrderNo, lineId, startTime, endTime);

            var book = new XSSFWorkbook();
            var sheet = book.CreateSheet(siteType.Replace("/", "") + "_" + (hasViaSite ? "已过站组件" : "未过站组件"));
            for (int i = 0; i <= 22; i++)
            {
                sheet.SetColumnWidth(i, 20 * 256 + 200);
            }
            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            var rowHeader = sheet.CreateRow(0);
            rowHeader.Height = 30 * 20;
            rowHeader.CreateCell(0).SetCellValue("序号");
            rowHeader.CreateCell(1).SetCellValue("组件条码");
            rowHeader.CreateCell(2).SetCellValue("订单号");
            rowHeader.CreateCell(3).SetCellValue("工单号");
            rowHeader.CreateCell(4).SetCellValue("成品料号");
            rowHeader.CreateCell(5).SetCellValue("成品名称");
            rowHeader.CreateCell(6).SetCellValue("生产车间");
            rowHeader.CreateCell(7).SetCellValue("最终站点");
            rowHeader.CreateCell(8).SetCellValue("分片时间");
            rowHeader.CreateCell(9).SetCellValue("焊接时间");
            rowHeader.CreateCell(10).SetCellValue("叠层时间");
            rowHeader.CreateCell(11).SetCellValue("EL1时间");
            rowHeader.CreateCell(12).SetCellValue("层压时间");
            rowHeader.CreateCell(13).SetCellValue("NQC时间");
            rowHeader.CreateCell(14).SetCellValue("装框时间");
            rowHeader.CreateCell(15).SetCellValue("清洗时间");
            rowHeader.CreateCell(16).SetCellValue("IV测试时间");
            rowHeader.CreateCell(17).SetCellValue("绝缘耐压时间");
            rowHeader.CreateCell(18).SetCellValue("EL2时间");
            rowHeader.CreateCell(19).SetCellValue("铭牌打印时间");
            rowHeader.CreateCell(20).SetCellValue("终检时间");
            rowHeader.CreateCell(21).SetCellValue("包装时间");
            rowHeader.CreateCell(22).SetCellValue("入库时间");
            var rowIndex = 1;
            foreach (var item in result)
            {
                var tmpRow = sheet.CreateRow(rowIndex);
                tmpRow.CreateCell(0).SetCellValue(rowIndex);
                tmpRow.CreateCell(1).SetCellValue(item.BarCode);
                tmpRow.CreateCell(2).SetCellValue(item.OrderNo);
                tmpRow.CreateCell(3).SetCellValue(item.WorkOrderNo);
                tmpRow.CreateCell(4).SetCellValue(item.ProductCode);
                tmpRow.CreateCell(5).SetCellValue(item.ProductName);
                tmpRow.CreateCell(6).SetCellValue(item.WorkshopName);
                tmpRow.CreateCell(7).SetCellValue(!string.IsNullOrWhiteSpace(item.CNLastSite) ? item.CNLastSite : (item.DistributeTime.HasValue ? "分片" : ""));
                tmpRow.CreateCell(8).SetCellValue(item.DistributeTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(9).SetCellValue(item.WeldingTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(10).SetCellValue(item.LaminatedTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(11).SetCellValue(item.EL1Time?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(12).SetCellValue(item.TopTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(13).SetCellValue(item.NQCTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(14).SetCellValue(item.FramingTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(15).SetCellValue(item.CleanTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(16).SetCellValue(item.IVTestTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(17).SetCellValue(item.InsulatedResistanceTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(18).SetCellValue(item.EL2Time?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(19).SetCellValue(item.NameplatePrintingTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(20).SetCellValue(item.FinalInspectionTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(21).SetCellValue(item.PackTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(22).SetCellValue(item.InboundTime?.ToString("yyyy/MM/dd HH:mm:ss"));

                for (int i = 0; i <= 22; i++)
                {
                    tmpRow.GetCell(i).CellStyle = styleCenter;
                }
                rowIndex++;
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", siteType.Replace("/", "") + "_" + (hasViaSite ? "已过站组件" : "未过站组件")));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
        }
        #endregion

        #region 打包报表
        /// <summary>
        /// 打包报表
        /// </summary>
        public ActionResult PackReport()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 查询打包报表
        /// </summary>
        [HttpPost]
        public JsonResult QueryPackReports(int page, int limit, string workshopCode, string orderNo, string workOrderNo, DateTime? startTime, DateTime? endTime)
        {
            var total = 0;
            var componentPacks = new ReportMgr().QueryComponentPacks(page, limit, out total, workshopCode, orderNo, workOrderNo, startTime, endTime);
            return Json(new ResultJson<object>(componentPacks, total));
        }

        /// <summary>
        /// 导出打包报表
        /// </summary>
        public void ExportPackReport(string workshopCode, string orderNo, string workOrderNo, DateTime? startTime, DateTime? endTime)
        {
            var total = 0;
            var componentPacks = new ReportMgr().QueryComponentPacks(1, 0, out total, workshopCode, orderNo, workOrderNo, startTime, endTime);
            var book = new XSSFWorkbook();

            var sheet = book.CreateSheet("打包报表");
            for (int i = 0; i <= 21; i++)
            {
                sheet.SetColumnWidth(i, 20 * 256 + 200);
            }
            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            var rowHeader = sheet.CreateRow(0);
            rowHeader.Height = 30 * 20;
            rowHeader.CreateCell(0).SetCellValue("序号");
            rowHeader.CreateCell(1).SetCellValue("条码");
            rowHeader.CreateCell(2).SetCellValue("订单号");
            rowHeader.CreateCell(3).SetCellValue("工单号");
            rowHeader.CreateCell(4).SetCellValue("打包班次");
            rowHeader.CreateCell(5).SetCellValue("效率");
            rowHeader.CreateCell(6).SetCellValue("铭牌型号");
            rowHeader.CreateCell(7).SetCellValue("额定功率");
            rowHeader.CreateCell(8).SetCellValue("档位");
            rowHeader.CreateCell(9).SetCellValue("等级");
            rowHeader.CreateCell(10).SetCellValue("Voc");
            rowHeader.CreateCell(11).SetCellValue("Isc");
            rowHeader.CreateCell(12).SetCellValue("Pmax");
            rowHeader.CreateCell(13).SetCellValue("Vpm");
            rowHeader.CreateCell(14).SetCellValue("Imp");
            rowHeader.CreateCell(15).SetCellValue("FF");
            rowHeader.CreateCell(16).SetCellValue("Ress");
            rowHeader.CreateCell(17).SetCellValue("Ressh");
            rowHeader.CreateCell(18).SetCellValue("SunRef");
            rowHeader.CreateCell(19).SetCellValue("转换效率");
            rowHeader.CreateCell(20).SetCellValue("包装编号");
            rowHeader.CreateCell(21).SetCellValue("包装时间");
            var rowIndex = 1;
            foreach (var item in componentPacks)
            {
                var tmpRow = sheet.CreateRow(rowIndex);
                tmpRow.CreateCell(0).SetCellValue(rowIndex);
                tmpRow.CreateCell(1).SetCellValue(item.BarCode);
                tmpRow.CreateCell(2).SetCellValue(item.OrderNo);
                tmpRow.CreateCell(3).SetCellValue(item.WorkOrderNo);
                tmpRow.CreateCell(4).SetCellValue(item.PackDuty);
                tmpRow.CreateCell(5).SetCellValue((double)item.CellEfficiency);
                tmpRow.CreateCell(6).SetCellValue(item.NameplateModel);
                tmpRow.CreateCell(7).SetCellValue((double)(item.PowerLevel ?? 0));
                tmpRow.CreateCell(8).SetCellValue(item.CurrentLevel);
                tmpRow.CreateCell(9).SetCellValue(item.QLevel);
                tmpRow.CreateCell(10).SetCellValue((double)item.Voc);
                tmpRow.CreateCell(11).SetCellValue((double)item.Isc);
                tmpRow.CreateCell(12).SetCellValue((double)item.Pmax);
                tmpRow.CreateCell(13).SetCellValue((double)item.Vpm);
                tmpRow.CreateCell(14).SetCellValue((double)item.Imp);
                tmpRow.CreateCell(15).SetCellValue((double)item.FF);
                tmpRow.CreateCell(16).SetCellValue((double)item.Ress);
                tmpRow.CreateCell(17).SetCellValue((double)item.Ressh);
                tmpRow.CreateCell(18).SetCellValue((double)item.SunRef);
                tmpRow.CreateCell(19).SetCellValue((double)item.ConversionEfficiency);
                tmpRow.CreateCell(20).SetCellValue(item.PackNo);
                tmpRow.CreateCell(21).SetCellValue(item.PackTime.ToString("yyyy/MM/dd HH:mm:ss"));

                for (int i = 0; i <= 19; i++)
                {
                    tmpRow.GetCell(i).CellStyle = styleCenter;
                }
                rowIndex++;
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"{DateTime.Now.ToString("yyMMddHHmmss")}_打包报表"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
        }
        #endregion

        #region 不良报表
        /// <summary>
        /// 不良报表
        /// </summary>
        public ActionResult BadReport()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 查询不良报表
        /// </summary>
        [HttpPost]
        public JsonResult QueryBadReports(int page, int limit, string workshopCode, string orderNo, string workOrderNo, DateTime? startTime, DateTime? endTime)
        {
            var total = 0;
            var report = new ReportMgr().QueryBadReports(page, limit, out total, workshopCode, orderNo, workOrderNo, startTime, endTime);
            return Json(new ResultJson<object>(report, total));
        }

        /// <summary>
        /// 导出不良报表
        /// </summary>
        public void ExportBadReports(string workshopCode, string orderNo, string workOrderNo, DateTime? startTime, DateTime? endTime)
        {
            var total = 0;
            var componentPacks = new ReportMgr().QueryBadReports(1, 0, out total, workshopCode, orderNo, workOrderNo, startTime, endTime);
            var book = new XSSFWorkbook();

            var sheet = book.CreateSheet("不良报表");
            for (int i = 0; i <= 22; i++)
            {
                sheet.SetColumnWidth(i, 20 * 256 + 200);
            }
            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            var rowHeader = sheet.CreateRow(0);
            rowHeader.Height = 30 * 20;
            rowHeader.CreateCell(0).SetCellValue("序号");
            rowHeader.CreateCell(1).SetCellValue("条码");
            rowHeader.CreateCell(2).SetCellValue("订单号");
            rowHeader.CreateCell(3).SetCellValue("工单号");
            rowHeader.CreateCell(4).SetCellValue("额定功率");
            rowHeader.CreateCell(5).SetCellValue("档位");
            rowHeader.CreateCell(6).SetCellValue("实际功率");
            rowHeader.CreateCell(7).SetCellValue("封装损耗");
            rowHeader.CreateCell(8).SetCellValue("EL2等级");
            rowHeader.CreateCell(9).SetCellValue("EL2原因");
            rowHeader.CreateCell(10).SetCellValue("EL2不良位置");
            rowHeader.CreateCell(11).SetCellValue("铭牌型号");
            rowHeader.CreateCell(12).SetCellValue("组件等级");
            rowHeader.CreateCell(13).SetCellValue("组件颜色");
            rowHeader.CreateCell(14).SetCellValue("组件类型");
            rowHeader.CreateCell(15).SetCellValue("倒角类型");
            rowHeader.CreateCell(16).SetCellValue("不良项");
            rowHeader.CreateCell(17).SetCellValue("来料不良");
            rowHeader.CreateCell(18).SetCellValue("备注");
            rowHeader.CreateCell(19).SetCellValue("操作员");
            rowHeader.CreateCell(20).SetCellValue("班次");
            rowHeader.CreateCell(21).SetCellValue("编号");
            rowHeader.CreateCell(22).SetCellValue("时间");
            rowHeader.CreateCell(23).SetCellValue("外来组件");
            var rowIndex = 1;
            foreach (var item in componentPacks)
            {
                var tmpRow = sheet.CreateRow(rowIndex);
                tmpRow.CreateCell(0).SetCellValue(rowIndex);
                tmpRow.CreateCell(1).SetCellValue(item.BarCode);
                tmpRow.CreateCell(2).SetCellValue(item.OrderNo);
                tmpRow.CreateCell(3).SetCellValue(item.WorkOrderNo);
                tmpRow.CreateCell(4).SetCellValue((double)item.PowerLevel);
                tmpRow.CreateCell(5).SetCellValue(item.CurrentLevel);
                tmpRow.CreateCell(6).SetCellValue((double)item.Power);
                tmpRow.CreateCell(7).SetCellValue((double)item.PackageLoss);
                tmpRow.CreateCell(8).SetCellValue(item.EL2Level);
                tmpRow.CreateCell(9).SetCellValue(item.EL2Reason);
                tmpRow.CreateCell(10).SetCellValue(item.EL2Position);
                tmpRow.CreateCell(11).SetCellValue(item.NameplateModel);
                tmpRow.CreateCell(12).SetCellValue(item.QLevel);
                tmpRow.CreateCell(13).SetCellValue(item.ComponentColor);
                tmpRow.CreateCell(14).SetCellValue(item.ComponentType);
                tmpRow.CreateCell(15).SetCellValue(item.CellFeature);
                tmpRow.CreateCell(16).SetCellValue(item.BadReason);
                tmpRow.CreateCell(17).SetCellValue(item.IsMeteriaComeBad ? "是" : "否");
                tmpRow.CreateCell(18).SetCellValue(item.Remark);
                tmpRow.CreateCell(19).SetCellValue(item.Operator);
                tmpRow.CreateCell(20).SetCellValue(item.DutyName);
                tmpRow.CreateCell(21).SetCellValue(item.No);
                tmpRow.CreateCell(22).SetCellValue(item.CreateTime?.ToString("yyyy/MM/dd HH:mm:ss"));
                tmpRow.CreateCell(23).SetCellValue(item.ComponentColor);

                for (int i = 0; i < 23; i++)
                {
                    tmpRow.GetCell(i).CellStyle = styleCenter;
                }
                rowIndex++;
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"{DateTime.Now.ToString("yyMMddHHmmss")}_不良报表"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
        }
        #endregion

        #region 工单查询
        public ActionResult WorkOrderMgr()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 查询工单列表
        /// </summary>
        public JsonResult QueryWorkOrders(int page, int limit, string workshopCode, string orderNo, string workOrderNo, ProduceStatus? status, DateTime? startDate, DateTime? endDate)
        {
            var total = 0;
            var workshopCodes = string.IsNullOrWhiteSpace(workshopCode) ? new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId).Select(_ => _.WorkshopCode).ToList() : new List<string> { workshopCode };
            var workOrders = new WorkOrderMgr().QueryWorkOrders(page, limit, out total, workshopCodes, orderNo, workOrderNo, status, startDate, endDate?.AddDays(1), true);
            return Json(new ResultJson<object>(workOrders, total));
        }

        /// <summary>
        /// 导出工单列表
        /// </summary>
        public void ExportWorkOrders(string workshopCode, string orderNo, string workOrderNo, DateTime? startDate, DateTime? endDate)
        {
            var book = new XSSFWorkbook();
            var sheet = book.CreateSheet("工单列表");
            for (int i = 0; i <= 23; i++)
            {
                sheet.SetColumnWidth(i, 20 * 256 + 200);
            }
            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            #region Header
            var rowHeader = sheet.CreateRow(0);
            rowHeader.Height = 30 * 20;
            rowHeader.CreateCell(0).SetCellValue("序号");
            rowHeader.CreateCell(1).SetCellValue("工单号");
            rowHeader.CreateCell(2).SetCellValue("订单号");
            rowHeader.CreateCell(3).SetCellValue("生产车间");
            rowHeader.CreateCell(4).SetCellValue("成品料号");
            rowHeader.CreateCell(5).SetCellValue("成品名称");
            rowHeader.CreateCell(6).SetCellValue("成品规格");
            rowHeader.CreateCell(7).SetCellValue("开工日期");
            rowHeader.CreateCell(8).SetCellValue("完工日期");
            rowHeader.CreateCell(9).SetCellValue("数量");
            rowHeader.CreateCell(10).SetCellValue("电池片数");
            rowHeader.CreateCell(11).SetCellValue("电池等级");
            rowHeader.CreateCell(12).SetCellValue("电池型号");
            rowHeader.CreateCell(13).SetCellValue("电池效率");
            rowHeader.CreateCell(14).SetCellValue("产出功率");
            rowHeader.CreateCell(15).SetCellValue("铭牌型号");
            rowHeader.CreateCell(16).SetCellValue("客户代码");
            rowHeader.CreateCell(17).SetCellValue("客户名称");
            rowHeader.CreateCell(18).SetCellValue("分片数量");
            rowHeader.CreateCell(19).SetCellValue("叠层数量");
            rowHeader.CreateCell(20).SetCellValue("打包数量");
            rowHeader.CreateCell(21).SetCellValue("入库数量");
            rowHeader.CreateCell(22).SetCellValue("入库达成率");
            rowHeader.CreateCell(23).SetCellValue("审核时间");
            rowHeader.CreateCell(24).SetCellValue("同步时间");
            #endregion

            if (string.IsNullOrWhiteSpace(workshopCode) && string.IsNullOrWhiteSpace(orderNo) && string.IsNullOrWhiteSpace(workOrderNo) && !startDate.HasValue && !endDate.HasValue)
            {
                var ms1 = new MemoryStream();
                book.Write(ms1);
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"{DateTime.Now.ToString("yyMMddHHmmss")}_工单查询"));
                Response.BinaryWrite(ms1.ToArray());
                book = null;
                ms1.Close();
                ms1.Dispose();
                return;
            }

            var total = 0;
            var workshopCodes = string.IsNullOrWhiteSpace(workshopCode) ? new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId).Select(_ => _.WorkshopCode).ToList() : new List<string> { workshopCode };
            var workOrders = new WorkOrderMgr().QueryWorkOrders(1, 0, out total, workshopCodes, orderNo, workOrderNo, null, startDate, endDate?.AddDays(1), true);

            var rowIndex = 1;
            foreach (var workOrder in workOrders)
            {
                var tmpRow = sheet.CreateRow(rowIndex);
                tmpRow.CreateCell(0).SetCellValue(rowIndex);
                tmpRow.CreateCell(1).SetCellValue(workOrder.WorkOrderNo);
                tmpRow.CreateCell(2).SetCellValue(workOrder.OrderNo);
                tmpRow.CreateCell(3).SetCellValue(workOrder.WorkshopName);
                tmpRow.CreateCell(4).SetCellValue(workOrder.ProductCode);
                tmpRow.CreateCell(5).SetCellValue(workOrder.ProductName);
                tmpRow.CreateCell(6).SetCellValue(workOrder.ProductSpecs);
                tmpRow.CreateCell(7).SetCellValue(workOrder.StartDate?.ToString("yyyy-MM-dd") ?? string.Empty);
                tmpRow.CreateCell(8).SetCellValue(workOrder.EndDate?.ToString("yyyy-MM-dd") ?? string.Empty);
                tmpRow.CreateCell(9).SetCellValue(workOrder.Count);
                tmpRow.CreateCell(10).SetCellValue(workOrder.CellQuantity?.ToString() ?? string.Empty);
                tmpRow.CreateCell(11).SetCellValue(workOrder.CellLevel);
                tmpRow.CreateCell(12).SetCellValue(workOrder.CellModel);
                tmpRow.CreateCell(13).SetCellValue(!workOrder.CellEfficiencyMin.HasValue ? "" : $"{workOrder.CellEfficiencyMin}-{workOrder.CellEfficiencyMax}");
                tmpRow.CreateCell(14).SetCellValue(string.Join("、", workOrder.OutputPowers.Select(_ => _.OutputPower)));
                tmpRow.CreateCell(15).SetCellValue(string.Join("、", workOrder.BinningPowers.Select(_ => _.NameplateModel)));
                tmpRow.CreateCell(16).SetCellValue(workOrder.CustomerCode);
                tmpRow.CreateCell(17).SetCellValue(workOrder.CustomerName);
                tmpRow.CreateCell(18).SetCellValue(workOrder.DistributeQuantity);
                tmpRow.CreateCell(19).SetCellValue(workOrder.LaminatedQuantity);
                tmpRow.CreateCell(20).SetCellValue(workOrder.PackQuantity);
                tmpRow.CreateCell(21).SetCellValue(workOrder.InboundQuantity);
                tmpRow.CreateCell(22).SetCellValue(workOrder.InboundRate.ToString("0.00"));
                tmpRow.CreateCell(23).SetCellValue(workOrder.ReviewTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? string.Empty);
                tmpRow.CreateCell(24).SetCellValue(workOrder.WorkOrderTime.ToString("yyyy-MM-dd HH:mm:ss"));

                for (int i = 0; i < 24; i++)
                {
                    tmpRow.GetCell(i).CellStyle = styleCenter;
                }
                rowIndex++;
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"{DateTime.Now.ToString("yyMMddHHmmss")}_工单查询"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
        }
        #endregion

        #region 计划报表
        /// <summary>
        /// 计划报表
        /// </summary>
        public ActionResult Statistics()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 查询计划报表
        /// </summary>
        [HttpPost]
        public JsonResult QueryStatistics(string workshopCode, string orderNo, string workOrderNo, bool needLevelPowers, DateTime? startTime, DateTime? endTime)
        {
            var result = new ReportMgr().QueryStatistics(workshopCode, orderNo, workOrderNo, needLevelPowers, startTime, endTime);
            return Json(new ResultJson<object>(new { Headers = result.Item1, Data = result.Item2 }));
        }

        /// <summary>
        /// 导出计划报表
        /// </summary>
        public void ExportStatistics(string workshopCode, string orderNo, string workOrderNo, bool needLevelPowers, DateTime? startTime, DateTime? endTime)
        {
            var result = new ReportMgr().QueryStatistics(workshopCode, orderNo, workOrderNo, needLevelPowers, startTime, endTime);
            var book = new XSSFWorkbook();
            var sheet = book.CreateSheet();
            for (int i = 0; i <= result.Item1.Count; i++)
            {
                sheet.SetColumnWidth(i, 8 * 400 + 200);
            }
            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            var rowHeader = sheet.CreateRow(0);
            rowHeader.Height = 30 * 20;
            rowHeader.CreateCell(0).SetCellValue("序号");
            foreach (var item in result.Item1)
            {
                rowHeader.CreateCell(result.Item1.IndexOf(item) + 1).SetCellValue(item);
            }

            foreach (var item in result.Item2)
            {
                var tmpRow = sheet.CreateRow(1 + result.Item2.IndexOf(item));
                tmpRow.Height = 30 * 20;
                tmpRow.CreateCell(0).SetCellValue(1 + result.Item2.IndexOf(item));

                var index = 1;
                foreach (var dict in item)
                {
                    tmpRow.CreateCell(index).SetCellValue(Convert.ToString(dict.Value));
                    index++;
                }
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"计划报表"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
        }
        #endregion

        #region CTM报表
        public ActionResult CTMReport()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 查询CTM报表
        /// </summary>
        [HttpPost]
        public JsonResult QueryCTMS(string workshopCode, string orderNo, string workOrderNo, DateTime? startTime, DateTime? endTime)
        {
            var ctms = new ReportMgr().QueryCTMs(workshopCode, orderNo, workOrderNo, startTime, endTime);
            return Json(new ResultJson<object>(ctms, ctms.Count));
        }

        /// <summary>
        /// 导出CTM报表
        /// </summary>
        public void ExportCTMS(string workshopCode, string orderNo, string workOrderNo, DateTime? startTime, DateTime? endTime)
        {
            var ctms = new ReportMgr().QueryCTMs(workshopCode, orderNo, workOrderNo, startTime, endTime);
            var book = new XSSFWorkbook();

            var sheet = book.CreateSheet("CTM报表");
            for (int i = 0; i <= 9; i++)
            {
                sheet.SetColumnWidth(i, 10 * 256 + 200);
            }
            sheet.SetColumnWidth(1, 15 * 256 + 200);
            sheet.SetColumnWidth(2, 20 * 256 + 200);
            sheet.SetColumnWidth(3, 20 * 256 + 200);
            sheet.SetColumnWidth(9, 40 * 256 + 200);
            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            var rowHeader = sheet.CreateRow(0);
            rowHeader.Height = 30 * 20;
            rowHeader.CreateCell(0).SetCellValue("序号");
            rowHeader.CreateCell(1).SetCellValue("订单号");
            rowHeader.CreateCell(2).SetCellValue("厂商");
            rowHeader.CreateCell(3).SetCellValue("型号");
            rowHeader.CreateCell(4).SetCellValue("效率");
            rowHeader.CreateCell(5).SetCellValue("理论功率");
            rowHeader.CreateCell(6).SetCellValue("测试数");
            rowHeader.CreateCell(7).SetCellValue("平均功率");
            rowHeader.CreateCell(8).SetCellValue("CTM");
            rowHeader.CreateCell(9).SetCellValue("描述");
            var rowIndex = 1;
            foreach (var item in ctms)
            {
                var tmpRow = sheet.CreateRow(rowIndex);
                tmpRow.CreateCell(0).SetCellValue(rowIndex);
                tmpRow.CreateCell(1).SetCellValue(item.OrderNo);
                tmpRow.CreateCell(2).SetCellValue(item.BatteryFactory);
                tmpRow.CreateCell(3).SetCellValue(item.BatterySpecs);
                tmpRow.CreateCell(4).SetCellValue((double)item.CellEfficiency);
                tmpRow.CreateCell(5).SetCellValue((double)item.TheoreticalPower);
                tmpRow.CreateCell(6).SetCellValue(item.Quantity);
                tmpRow.CreateCell(7).SetCellValue((double)item.AveragePower);
                tmpRow.CreateCell(8).SetCellValue((double)item.CTM);
                tmpRow.CreateCell(9).SetCellValue(item.Desc);

                for (int i = 0; i <= 9; i++)
                {
                    tmpRow.GetCell(i).CellStyle = styleCenter;
                }
                rowIndex++;
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"{DateTime.Now.ToString("yyMMddHHmmss")}_CTM报表"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
        }
        #endregion

        #region 分拣记录
        /// <summary>
        /// 分拣记录
        /// </summary>
        public ActionResult SortRecords()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 查询分拣记录
        /// </summary>
        [HttpPost]
        public JsonResult QuerySortRecords(int page, int limit, string workshopCode, int lineId, int stackNo,
            DateTime? startTime, DateTime? endTime, string barCode, SortRecordStatus? status)
        {
            var result = new SortMgr().QuerySortRecords(page, limit, workshopCode, lineId, stackNo, startTime, endTime, barCode, status);
            return Json(new ResultJson<object>(result.Item1, result.Item2));
        }

        /// <summary>
        /// 导出分拣记录
        /// </summary>
        public void ExportSortRecords(string workshopCode, int lineId, int stackNo, DateTime? startTime, DateTime? endTime, string barCode, SortRecordStatus? status)
        {
            var book = new XSSFWorkbook();

            var sheet = book.CreateSheet("分拣记录");
            for (int i = 0; i <= 15; i++)
            {
                sheet.SetColumnWidth(i, 10 * 256 + 200);
            }
            sheet.SetColumnWidth(1, 25 * 256 + 200);
            sheet.SetColumnWidth(2, 15 * 256 + 200);
            sheet.SetColumnWidth(3, 15 * 256 + 200);
            sheet.SetColumnWidth(6, 15 * 256 + 200);
            sheet.SetColumnWidth(11, 20 * 256 + 200);
            sheet.SetColumnWidth(15, 20 * 256 + 200);
            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            var rowHeader = sheet.CreateRow(0);
            rowHeader.Height = 30 * 20;
            rowHeader.CreateCell(0).SetCellValue("序号");
            rowHeader.CreateCell(1).SetCellValue("条码");
            rowHeader.CreateCell(2).SetCellValue("工单号");
            rowHeader.CreateCell(3).SetCellValue("订单号");
            rowHeader.CreateCell(4).SetCellValue("车间");
            rowHeader.CreateCell(5).SetCellValue("流水线");
            rowHeader.CreateCell(6).SetCellValue("设备");
            rowHeader.CreateCell(7).SetCellValue("堆号");
            rowHeader.CreateCell(8).SetCellValue("满堆数量");
            rowHeader.CreateCell(9).SetCellValue("当前块");
            rowHeader.CreateCell(10).SetCellValue("状态");
            rowHeader.CreateCell(11).SetCellValue("分拣时间");
            rowHeader.CreateCell(12).SetCellValue("功率档");
            rowHeader.CreateCell(13).SetCellValue("电流档");
            rowHeader.CreateCell(14).SetCellValue("等级");
            rowHeader.CreateCell(15).SetCellValue("铭牌型号");

            var result = new SortMgr().QuerySortRecords(1, 0, workshopCode, lineId, stackNo, startTime, endTime, barCode, status);
            if (result.Item1.Count <= 0)
            {
                var tmpStream = new MemoryStream();
                book.Write(tmpStream);
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"{DateTime.Now.ToString("yyMMddHHmmss")}_分拣记录"));
                Response.BinaryWrite(tmpStream.ToArray());
                book = null;
                tmpStream.Close();
                tmpStream.Dispose();
                return;
            }

            var rowIndex = 1;
            foreach (var item in result.Item1)
            {
                var tmpRow = sheet.CreateRow(rowIndex);
                tmpRow.CreateCell(0).SetCellValue(rowIndex);
                tmpRow.CreateCell(1).SetCellValue(item.BarCode);
                tmpRow.CreateCell(2).SetCellValue(item.WorkOrderNo);
                tmpRow.CreateCell(3).SetCellValue(item.OrderNo);
                tmpRow.CreateCell(4).SetCellValue(item.WorkshopName);
                tmpRow.CreateCell(5).SetCellValue(item.LineName);
                tmpRow.CreateCell(6).SetCellValue(item.MachineName);
                tmpRow.CreateCell(7).SetCellValue(item.StackNo);
                tmpRow.CreateCell(8).SetCellValue(item.FullCount);
                tmpRow.CreateCell(9).SetCellValue(item.SortNo);
                tmpRow.CreateCell(10).SetCellValue(item.CNStatus);
                tmpRow.CreateCell(11).SetCellValue(item.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                tmpRow.CreateCell(12).SetCellValue(item.PowerLevel?.ToString() ?? string.Empty);
                tmpRow.CreateCell(13).SetCellValue(item.CurrentLevel);
                tmpRow.CreateCell(14).SetCellValue(item.QLevel);
                tmpRow.CreateCell(15).SetCellValue(item.NameplateModel);

                for (int i = 0; i <= 15; i++)
                {
                    tmpRow.GetCell(i).CellStyle = styleCenter;
                }
                rowIndex++;
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"{DateTime.Now.ToString("yyMMddHHmmss")}_分拣记录"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
        }

        /// <summary>
        /// 分拣堆
        /// </summary>
        public ActionResult SortStacks()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            return View();
        }

        /// <summary>
        /// 查询分拣堆
        /// </summary>
        [HttpPost]
        public JsonResult QuerySortStacks(int page, int limit, string workshopCode, int lineId, int stackNo, DateTime? startTime, DateTime? endTime)
        {
            var result = new SortMgr().QueryStacks(page, limit, workshopCode, lineId, stackNo, startTime, endTime);
            return Json(new ResultJson<object>(result.Item1, result.Item2));
        }

        /// <summary>
        /// 导出分拣堆
        /// </summary>
        public void ExporSortStacks(string workshopCode, int lineId, int stackNo, DateTime? startTime, DateTime? endTime)
        {
            var book = new XSSFWorkbook();

            var sheet = book.CreateSheet("分拣堆");
            for (int i = 0; i <= 8; i++)
            {
                sheet.SetColumnWidth(i, 10 * 256 + 200);
            }
            sheet.SetColumnWidth(3, 20 * 256 + 200);
            sheet.SetColumnWidth(8, 20 * 256 + 200);
            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            var rowHeader = sheet.CreateRow(0);
            rowHeader.Height = 30 * 20;
            rowHeader.CreateCell(0).SetCellValue("序号");
            rowHeader.CreateCell(1).SetCellValue("车间");
            rowHeader.CreateCell(2).SetCellValue("流水线");
            rowHeader.CreateCell(3).SetCellValue("设备");
            rowHeader.CreateCell(4).SetCellValue("堆号");
            rowHeader.CreateCell(5).SetCellValue("满堆数量");
            rowHeader.CreateCell(6).SetCellValue("总块数");
            rowHeader.CreateCell(7).SetCellValue("状态");
            rowHeader.CreateCell(8).SetCellValue("创建时间");

            var result = new SortMgr().QueryStacks(1, 0, workshopCode, lineId, stackNo, startTime, endTime);
            if (result.Item1.Count <= 0)
            {
                var tmpStream = new MemoryStream();
                book.Write(tmpStream);
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"{DateTime.Now.ToString("yyMMddHHmmss")}_分拣堆"));
                Response.BinaryWrite(tmpStream.ToArray());
                book = null;
                tmpStream.Close();
                tmpStream.Dispose();
                return;
            }

            var rowIndex = 1;
            foreach (var item in result.Item1)
            {
                var tmpRow = sheet.CreateRow(rowIndex);
                tmpRow.CreateCell(0).SetCellValue(rowIndex);
                tmpRow.CreateCell(1).SetCellValue(item.WorkshopName);
                tmpRow.CreateCell(2).SetCellValue(item.LineName);
                tmpRow.CreateCell(3).SetCellValue(item.MachineName);
                tmpRow.CreateCell(4).SetCellValue(item.StackNo);
                tmpRow.CreateCell(5).SetCellValue(item.FullCount);
                tmpRow.CreateCell(6).SetCellValue(item.SortNo);
                tmpRow.CreateCell(7).SetCellValue(item.CNStatus);
                tmpRow.CreateCell(8).SetCellValue(item.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));

                for (int i = 0; i <= 8; i++)
                {
                    tmpRow.GetCell(i).CellStyle = styleCenter;
                }
                rowIndex++;
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"{DateTime.Now.ToString("yyMMddHHmmss")}_分拣堆"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
        }

        /// <summary>
        /// 分拣堆组件列表
        /// </summary>
        public ActionResult SortStackRecord(int stackId)
        {
            var records = new SortMgr().QuerySortRecords(stackId);
            return View(records);
        }
        #endregion

        #region 物料报表
        /// <summary>
        /// 八大主材物料报表
        /// </summary>
        public ActionResult MateriaReport()
        {
            return View();
        }

        /// <summary>
        /// 查询物料报表
        /// </summary>
        [HttpPost]
        public JsonResult QueryMaterioaReport(int page, int limit, string orderNo, string workOrderNo, string cabinetNo, string packNo, string barCode)
        {
            var result = new ReportMgr().QueryMaterioaReport(page, limit, orderNo, workOrderNo, cabinetNo, packNo, barCode);
            return Json(new ResultJson<object>(result.Item2, result.Item1));
        }


        /// <summary>
        /// 导出物料报表
        /// </summary>
        public void ExportMaterioaReport(string orderNo, string workOrderNo, string cabinetNo, string packNo, string barCode)
        {
            var result = new ReportMgr().QueryMaterioaReport(0, 0, orderNo, workOrderNo, cabinetNo, packNo, barCode);
            var book = new XSSFWorkbook();

            var sheet = book.CreateSheet("物料报表");
            for (int i = 0; i <= 40; i++)
            {
                sheet.SetColumnWidth(i, 15 * 256 + 200);
            }
            sheet.SetColumnWidth(0, 8 * 256 + 200);
            sheet.SetColumnWidth(1, 25 * 256 + 200);
            sheet.SetColumnWidth(2, 20 * 256 + 200);
            sheet.SetColumnWidth(3, 20 * 256 + 200);
            sheet.SetColumnWidth(5, 20 * 256 + 200);
            sheet.SetColumnWidth(6, 20 * 256 + 200);
            sheet.SetColumnWidth(7, 20 * 256 + 200);
            sheet.SetColumnWidth(8, 20 * 256 + 200);
            var styleCenter = book.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.BorderBottom = BorderStyle.Thin;
            styleCenter.BorderLeft = BorderStyle.Thin;
            styleCenter.BorderRight = BorderStyle.Thin;
            styleCenter.BorderTop = BorderStyle.Thin;

            var rowHeader = sheet.CreateRow(0);
            rowHeader.Height = 30 * 20;
            rowHeader.CreateCell(0).SetCellValue("序号");
            rowHeader.CreateCell(1).SetCellValue("组件条码");
            rowHeader.CreateCell(2).SetCellValue("生产车间");
            rowHeader.CreateCell(3).SetCellValue("订单号");
            rowHeader.CreateCell(4).SetCellValue("工单号");
            rowHeader.CreateCell(5).SetCellValue("柜号");
            rowHeader.CreateCell(6).SetCellValue("箱号");
            rowHeader.CreateCell(7).SetCellValue("成品料号");
            rowHeader.CreateCell(8).SetCellValue("成品规格");
            rowHeader.CreateCell(9).SetCellValue("电池片料号");
            rowHeader.CreateCell(10).SetCellValue("电池片规格");
            rowHeader.CreateCell(11).SetCellValue("电池片厂家");
            rowHeader.CreateCell(12).SetCellValue("电池片批次号");
            rowHeader.CreateCell(13).SetCellValue("玻璃料号");
            rowHeader.CreateCell(14).SetCellValue("玻璃规格");
            rowHeader.CreateCell(15).SetCellValue("玻璃厂家");
            rowHeader.CreateCell(16).SetCellValue("玻璃批次号");
            rowHeader.CreateCell(17).SetCellValue("背板料号");
            rowHeader.CreateCell(18).SetCellValue("背板规格");
            rowHeader.CreateCell(19).SetCellValue("背板厂家");
            rowHeader.CreateCell(20).SetCellValue("背板批次号");
            rowHeader.CreateCell(21).SetCellValue("EVA料号");
            rowHeader.CreateCell(22).SetCellValue("EVA规格");
            rowHeader.CreateCell(23).SetCellValue("EVA厂家");
            rowHeader.CreateCell(24).SetCellValue("EVA批次号");
            rowHeader.CreateCell(25).SetCellValue("焊带料号");
            rowHeader.CreateCell(26).SetCellValue("焊带规格");
            rowHeader.CreateCell(27).SetCellValue("焊带厂家");
            rowHeader.CreateCell(28).SetCellValue("焊带批次号");
            rowHeader.CreateCell(29).SetCellValue("长边框料号");
            rowHeader.CreateCell(30).SetCellValue("长边框规格");
            rowHeader.CreateCell(31).SetCellValue("长边框厂家");
            rowHeader.CreateCell(32).SetCellValue("长边框批次号");
            rowHeader.CreateCell(33).SetCellValue("短边框料号");
            rowHeader.CreateCell(34).SetCellValue("短边框规格");
            rowHeader.CreateCell(35).SetCellValue("短边框厂家");
            rowHeader.CreateCell(36).SetCellValue("短边框批次号");
            rowHeader.CreateCell(37).SetCellValue("接线盒料号");
            rowHeader.CreateCell(38).SetCellValue("接线盒规格");
            rowHeader.CreateCell(39).SetCellValue("接线盒厂家");
            rowHeader.CreateCell(40).SetCellValue("接线盒批次号");
            var rowIndex = 1;
            foreach (var item in result.Item2)
            {
                var tmpRow = sheet.CreateRow(rowIndex);
                tmpRow.CreateCell(0).SetCellValue(rowIndex);
                tmpRow.CreateCell(1).SetCellValue(item.BarCode);
                tmpRow.CreateCell(2).SetCellValue(item.WorkshopName);
                tmpRow.CreateCell(3).SetCellValue(item.OrderNo);
                tmpRow.CreateCell(4).SetCellValue(item.WorkOrderNo);
                tmpRow.CreateCell(5).SetCellValue(item.CabinetNo);
                tmpRow.CreateCell(6).SetCellValue(item.PackNo);
                tmpRow.CreateCell(7).SetCellValue(item.ProductCode);
                tmpRow.CreateCell(8).SetCellValue(item.ProductSpecs);
                tmpRow.CreateCell(9).SetCellValue(item.BatteryCode);
                tmpRow.CreateCell(10).SetCellValue(item.BatterySpecs);
                tmpRow.CreateCell(11).SetCellValue(item.BatteryCompany);
                tmpRow.CreateCell(12).SetCellValue(item.BatteryBatchNo);
                tmpRow.CreateCell(13).SetCellValue(item.GlassCode);
                tmpRow.CreateCell(14).SetCellValue(item.GlassSpecs);
                tmpRow.CreateCell(15).SetCellValue(item.GlassCompany);
                tmpRow.CreateCell(16).SetCellValue(item.GlassBatchNo);
                tmpRow.CreateCell(17).SetCellValue(item.BackPlanCode);
                tmpRow.CreateCell(18).SetCellValue(item.BackPlanSpecs);
                tmpRow.CreateCell(19).SetCellValue(item.BackPlanCompany);
                tmpRow.CreateCell(20).SetCellValue(item.BackPlanBatchNo);
                tmpRow.CreateCell(21).SetCellValue(item.EvaCode);
                tmpRow.CreateCell(22).SetCellValue(item.EvaSpecs);
                tmpRow.CreateCell(23).SetCellValue(item.EvaCompany);
                tmpRow.CreateCell(24).SetCellValue(item.EvaBatchNo);
                tmpRow.CreateCell(25).SetCellValue(item.RibbonCode);
                tmpRow.CreateCell(26).SetCellValue(item.RibbonSpecs);
                tmpRow.CreateCell(27).SetCellValue(item.RibbonCompany);
                tmpRow.CreateCell(28).SetCellValue(item.RibbonBatchNo);
                tmpRow.CreateCell(29).SetCellValue(item.LongFrameCode);
                tmpRow.CreateCell(30).SetCellValue(item.LongFrameSpecs);
                tmpRow.CreateCell(31).SetCellValue(item.LongFrameCompany);
                tmpRow.CreateCell(32).SetCellValue(item.LongFrameBatchNo);
                tmpRow.CreateCell(33).SetCellValue(item.ShortFrameCode);
                tmpRow.CreateCell(34).SetCellValue(item.ShortFrameSpecs);
                tmpRow.CreateCell(35).SetCellValue(item.ShortFrameCompany);
                tmpRow.CreateCell(36).SetCellValue(item.ShortFrameBatchNo);
                tmpRow.CreateCell(37).SetCellValue(item.BoxCode);
                tmpRow.CreateCell(38).SetCellValue(item.BoxSpecs);
                tmpRow.CreateCell(39).SetCellValue(item.BoxCompany);
                tmpRow.CreateCell(40).SetCellValue(item.BoxBatchNo);

                for (int i = 0; i <= 40; i++)
                {
                    tmpRow.GetCell(i).CellStyle = styleCenter;
                }
                rowIndex++;
            }

            var ms = new MemoryStream();
            book.Write(ms);
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", $"{DateTime.Now.ToString("yyMMddHHmmss")}_物料报表"));
            Response.BinaryWrite(ms.ToArray());
            book = null;
            ms.Close();
            ms.Dispose();
        }
        #endregion
    }
}
